可以将 7-zip 的标准输出提供给 PHP 脚本吗?

发布于 2024-10-10 15:25:32 字数 1140 浏览 3 评论 0原文

我想使用 7-Zip 压缩目录,然后将其下载到网络浏览器。

但是,不是压缩到文件,而是让 PHP 将该文件的内容提供给浏览器...我理想情况下希望让 7-Zip 将新存档的内容写入标准输出,因为存档是正在创建...同时让 PHP 刷新其缓冲区并将数据发送到浏览器。

你看,我正在创建的档案相当大。从用户的角度来看,使用正常方法,当他们单击“下载”时,首先会创建存档并将其保存到文件中(这可能需要大约 10 分钟),然后才由 PHP 加载文件以供下载。我想做的是将归档和输出步骤合并到一个进程中。

有人对此有任何意见吗?

SourceForge 上的其他人正在尝试做同样的事情...

http://sourceforge.net/projects/p7zip/forums/forum/383043/topic/3828566

=======

感谢您的所有帮助。我将关闭这个线程,因为我最近发现 StackOverflow 上已经存在关于这个主题的精彩讨论。请参阅:

LAMP:如何创建 .为用户动态压缩大文件,无需磁盘/CPU 抖动

=======

这是我采用的最终解决方案,它对我来说效果非常好。请注意,我使用的是“InfoZip”程序,您几乎可以在任何平台上找到该程序。

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");

I would like to compress a directory using 7-Zip and then download it to a web browser.

However, instead of compressing to a file, then having PHP feed the contents of that file to the browser... I would ideally like to Have 7-Zip write the contents of the new archive to standard output as the archive is being created... While simultaneously having PHP flush its buffer and and send the data to the browser.

You see, the archive I'm creating is quite large. From a user's perspective, using the normal method, when they click "Download," the archive is first created and saved to a file (which can take ~ 10 minutes), and only then is the file then fed up by PHP to be downloaded. What I am trying to do is combine both the archive and output steps into one single process.

Does anyone have any input on this?

Here is someone else on SourceForge who is trying to do the same thing...

http://sourceforge.net/projects/p7zip/forums/forum/383043/topic/3828566

=======

Thank you for all help. I am going to close this thread, as I have recently discovered an excellent discussion on this topic already exists on StackOverflow. See:

LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing

=======

This is the final solution I went with, and it is working fantastic for me. Note that I am using the "InfoZip" program, which you can find for pretty much any platform.

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謌踐踏愛綪 2024-10-17 15:25:33

您可以尝试使用 passthru 和 -so(将数据写入 stdout)选项来运行 7z。

只要您发送 7zip mime 类型的下载标头和内容类型标头,我相信它会起作用。

示例

<?php
$filename = "somefile.7z";
$contentType = "application/x-7z-compressed";
header("Content-type: ".$contentType); // tell the browser your sending a 7zip file
header("Content-Disposition: attachment; filename=\"".$filename."\""); // use 'attachment' to force a download
passthru('/path/to/7z a -so <fileOrDirToBe7Zipped>');
?>

希望有帮助!

You could try running 7z using passthru with the -so (write data to stdout) option.

As long as you are sending download headers and content-type headers for 7zip mime types, I believe it will work.

Example

<?php
$filename = "somefile.7z";
$contentType = "application/x-7z-compressed";
header("Content-type: ".$contentType); // tell the browser your sending a 7zip file
header("Content-Disposition: attachment; filename=\"".$filename."\""); // use 'attachment' to force a download
passthru('/path/to/7z a -so <fileOrDirToBe7Zipped>');
?>

Hope that helps!

梦里南柯 2024-10-17 15:25:33

这是我最终采用的解决方案,它对我来说效果非常好。请注意,我使用的是“InfoZip”程序,您几乎可以在任何平台上找到该程序。

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");

This is the final solution I went with, and it is working fantastic for me. Note that I am using the "InfoZip" program, which you can find for pretty much any platform.

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename="file.zip"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

passthru("zip -8 -q -r - ./some_directory | cat");
旧伤还要旧人安 2024-10-17 15:25:33

这要么不起作用,要么会出现严重的错误。网络实际上并不是这样工作的——许多浏览器将无法正确处理不完整的文件(这就是他们将得到的文件)。为什么不显示“您的文件正在生成!完成后我们将通过电子邮件向您发送下载链接。”消息,或“请稍候,我们正在生成您的文件。这可能需要几分钟时间。”消息,以及进度表?

This is either not going to work or it will be horribly buggy. The web doesn't really work like this -- many browsers won't be able to correctly deal with an incomplete file (which is what they're going to be getting). Why not display a "Your file is being generated! We will email you a download link when it is finished." message, or a "Please wait, we are generating your file. This could take several minutes." message, along with a progress meter?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文