如何在PHP中快速压缩大文件
我编写了一个 PHP 脚本来动态地将客户端选择的文件打包为 zip 文件并强制下载。它的效果很好,只是当文件数量很大(比如超过 50000 个)时,下载对话框需要很长时间才能出现在客户端。
我想过使用缓存来改进这个(这些文件不经常更改),但是因为文件的选择完全由用户决定,并且选择上有数以万计的组合,所以缓存组合非常困难。我还考虑过首先为各个文件生成 zip 存档,然后即时组合 zip 文件。但我没有找到在 PHP 中连接 zip 文件的方法。我能想到的另一种方法是在生成 zip 文件的同时发送(即读取)它。我也不知道这个是否支持。
如果有人可以在这方面帮助我,我将非常感谢您的帮助。
I wrote a PHP script to dynamically pack files selected by the client into zip file and force a download. It works well except that when the number of files is huge (like over 50000), it takes a very long time for the download dialog box to appear on the client side.
I thought about improving this using cache (these files are not changed very often), but because the selection of the files are totally decided by the user, and there are tens of thousands of combinations on the selection, it is very hard to cache combinations. I also thought about generating zip archives for individual files first, and then combining the zip files on-the-fly. But I did't find a way to concatenate zip files in PHP. Another way I can think of is sending (i.e., reading) the zip file at the same time as generating it. I also don't know if this is supported.
If someone could help me on this, I would really appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了扩展 Mike Sherov 的答案,请尝试结合使用 Tar 和 Gzip/Zip。使用 Gzip/Zip 单独预压缩所有文件,然后当客户做出选择时,您只需将这些文件打包在一起即可。这样,您仍然可以获得压缩的好处和下载一个文件的简单性,但不会产生与实时压缩大文件相关的开销和延迟。
To extened Mike Sherov's answer, try using a combination of Tar and Gzip/Zip. Individually pre-compress all the files using Gzip/Zip, Then when the client makes their selection, you simply Tar those files together. That way you still get the benefit of compression and the simplicity of downloading one file, but none of the overheads and delays associated with compressing large files in real time.
虽然这不是灵丹妙药,但您可以尝试对文件进行 tar 处理。生成的文件更大,但压缩时间要短得多。请参阅此处了解更多信息:http://birdhouse.org /blog/2010/03/08/zip-vs-tar-gzip/
While not a silver bullet, you can try tar'ing the files instead. The resulting file is larger, but compression time is much shorter. See here for more info: http://birdhouse.org/blog/2010/03/08/zip-vs-tar-gzip/
查看 Nginx 的 mod_zip:
https://github.com/evanmiller/mod_zip
它将 ZIP 文件传输到客户端动态地运行,并且可以在使用很少的 RAM 的情况下包含非常大(2GB+)的文件。
Check out mod_zip for Nginx:
https://github.com/evanmiller/mod_zip
It streams a ZIP file to the client dynamically and can include very large (2GB+) files while using very little RAM.