英文:
Create and Download Zip File (maennchen/ZipStream-PHP)
问题
在我的项目中,我们使用了maennchen/ZipStream-PHP bundle。现在我正在尝试创建和下载zip文件,但是当我打开zip文件时,出现了"empty archive"错误。
return new StreamedResponse(function() use ($filenames, $logger) {
$filename = 'example.zip';
$opt = new ArchiveOptions();
$opt->setContentDisposition("attachment;filename=\"$filename\"");
$opt->setContentType('application/octet-stream');
$opt->setEnableZip64(false);
$opt->setFlushOutput(true);
$opt->setSendHttpHeaders(true);
$zip = new ZipStream($filename, $opt);
foreach ($filenames as $f) {
if (true === file_exists($f)) {
try {
$zip->addFileFromPath(basename($f), $f);
} catch (\Exception $exception) {
$logger->error($exception->getMessage());
}
} else {
$logger->error(sprintf('File %s does not exist.', $f));
}
}
$zip->finish();
});
如果我倒出$zip,那么文件参数是空的。我认为addFile方法不起作用。
你能帮助我吗?
提前感谢。
英文:
In my project we used maennchen/ZipStream-PHP bundle. Now I'm trying to create and download zip file, but when I open the zip file I have an error "empty archive".
return new StreamedResponse(function() use ($filenames, $logger) {
$filename = 'example.zip';
$opt = new ArchiveOptions();
$opt->setContentDisposition("attachment;filename=\"$filename\"");
$opt->setContentType('application/octet-stream');
$opt->setEnableZip64(false);
$opt->setFlushOutput(true);
$opt->setSendHttpHeaders(true);
$zip = new ZipStream($filename, $opt);
foreach ($filenames as $f) {
if (true === file_exists($f)) {
try {
$zip->addFileFromPath(basename($f), $f);
} catch (\Exception $exception) {
$logger->error($exception->getMessage());
}
} else {
$logger->error(sprintf('File %s does not exist.', $f));
}
}
$zip->finish();
});
If I dump $zip then the file argument is empty. I think the addFile method does not work.
Can you help me?
Thanks in advance.
答案1
得分: 3
Symfony的StreamedResponse已经发送了头部,所以你不应该在"stream"内部覆盖它们
尝试这样做:
return new StreamedResponse(function() use ($filenames, $logger) {
$opt = new \ZipStream\Option\Archive();
$opt->setZeroHeader(true);
$opt->setEnableZip64(false);
$zip = new ZipStream(null, $opt);
foreach ($filenames as $f) {
if (true === file_exists($f)) {
try {
$zip->addFileFromPath(basename($f), $f);
} catch (\Exception $exception) {
$logger->error($exception->getMessage());
}
} else {
$logger->error(sprintf('File %s does not exist.', $f));
}
}
$zip->finish();
},
200,
[
'Content-Disposition' => 'attachment;filename="example.zip"',
'Content-Type' => 'application/octet-stream',
]);
英文:
Symfony's StreamedResponse is sending the headers already, so you should not override them inside the "stream"
Try this;
return new StreamedResponse(function() use ($filenames, $logger) {
$opt = new \ZipStream\Option\Archive();
$opt->setZeroHeader(true);
$opt->setEnableZip64(false);
$zip = new ZipStream(null, $opt);
foreach ($filenames as $f) {
if (true === file_exists($f)) {
try {
$zip->addFileFromPath(basename($f), $f);
} catch (\Exception $exception) {
$logger->error($exception->getMessage());
}
} else {
$logger->error(sprintf('File %s does not exist.', $f));
}
}
$zip->finish();
},
200,
[
'Content-Disposition' => 'attachment;filename="example.zip"',
'Content-Type' => 'application/octet-stream',
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论