创建并下载 Zip 文件 (maennchen/ZipStream-PHP)

huangapple go评论131阅读模式
英文:

Create and Download Zip File (maennchen/ZipStream-PHP)

问题

在我的项目中,我们使用了maennchen/ZipStream-PHP bundle。现在我正在尝试创建和下载zip文件,但是当我打开zip文件时,出现了"empty archive"错误。

  1. return new StreamedResponse(function() use ($filenames, $logger) {
  2. $filename = 'example.zip';
  3. $opt = new ArchiveOptions();
  4. $opt->setContentDisposition("attachment;filename=\"$filename\"");
  5. $opt->setContentType('application/octet-stream');
  6. $opt->setEnableZip64(false);
  7. $opt->setFlushOutput(true);
  8. $opt->setSendHttpHeaders(true);
  9. $zip = new ZipStream($filename, $opt);
  10. foreach ($filenames as $f) {
  11. if (true === file_exists($f)) {
  12. try {
  13. $zip->addFileFromPath(basename($f), $f);
  14. } catch (\Exception $exception) {
  15. $logger->error($exception->getMessage());
  16. }
  17. } else {
  18. $logger->error(sprintf('File %s does not exist.', $f));
  19. }
  20. }
  21. $zip->finish();
  22. });

如果我倒出$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".

  1. return new StreamedResponse(function() use ($filenames, $logger) {
  2. $filename = 'example.zip';
  3. $opt = new ArchiveOptions();
  4. $opt->setContentDisposition("attachment;filename=\"$filename\"");
  5. $opt->setContentType('application/octet-stream');
  6. $opt->setEnableZip64(false);
  7. $opt->setFlushOutput(true);
  8. $opt->setSendHttpHeaders(true);
  9. $zip = new ZipStream($filename, $opt);
  10. foreach ($filenames as $f) {
  11. if (true === file_exists($f)) {
  12. try {
  13. $zip->addFileFromPath(basename($f), $f);
  14. } catch (\Exception $exception) {
  15. $logger->error($exception->getMessage());
  16. }
  17. } else {
  18. $logger->error(sprintf('File %s does not exist.', $f));
  19. }
  20. }
  21. $zip->finish();
  22. });

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"内部覆盖它们

尝试这样做:

  1. return new StreamedResponse(function() use ($filenames, $logger) {
  2. $opt = new \ZipStream\Option\Archive();
  3. $opt->setZeroHeader(true);
  4. $opt->setEnableZip64(false);
  5. $zip = new ZipStream(null, $opt);
  6. foreach ($filenames as $f) {
  7. if (true === file_exists($f)) {
  8. try {
  9. $zip->addFileFromPath(basename($f), $f);
  10. } catch (\Exception $exception) {
  11. $logger->error($exception->getMessage());
  12. }
  13. } else {
  14. $logger->error(sprintf('File %s does not exist.', $f));
  15. }
  16. }
  17. $zip->finish();
  18. },
  19. 200,
  20. [
  21. 'Content-Disposition' => 'attachment;filename="example.zip"',
  22. 'Content-Type' => 'application/octet-stream',
  23. ]);
英文:

Symfony's StreamedResponse is sending the headers already, so you should not override them inside the "stream"

Try this;

  1. return new StreamedResponse(function() use ($filenames, $logger) {
  2. $opt = new \ZipStream\Option\Archive();
  3. $opt->setZeroHeader(true);
  4. $opt->setEnableZip64(false);
  5. $zip = new ZipStream(null, $opt);
  6. foreach ($filenames as $f) {
  7. if (true === file_exists($f)) {
  8. try {
  9. $zip->addFileFromPath(basename($f), $f);
  10. } catch (\Exception $exception) {
  11. $logger->error($exception->getMessage());
  12. }
  13. } else {
  14. $logger->error(sprintf('File %s does not exist.', $f));
  15. }
  16. }
  17. $zip->finish();
  18. },
  19. 200,
  20. [
  21. 'Content-Disposition' => 'attachment;filename="example.zip"',
  22. 'Content-Type' => 'application/octet-stream',
  23. ]);

huangapple
  • 本文由 发表于 2020年1月3日 18:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定