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

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

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',
    ]);

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:

确定