如何在PHP中快速压缩大型文件 [英] How to quickly zip large files in PHP

查看:232
本文介绍了如何在PHP中快速压缩大型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个PHP脚本,将客户端选择的文件动态打包为zip文件并强制下载。效果很好,只是当文件数量很大(例如超过50000)时,下载对话框出现在客户端上需要很长时间。

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.

我考虑过使用缓存来改善此情况(这些文件不会经常更改),但是由于文件的选择完全由用户决定,并且选择中有成千上万的组合,因此很难缓存组合。我还考虑过先为单个文件生成zip归档文件,然后动态组合zip文件。但是我找不到在PHP中串联zip文件的方法。我能想到的另一种方法是在生成zip文件的同时发送(即读取)该zip文件。我也不知道是否支持。

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.

推荐答案

要扩展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.

这篇关于如何在PHP中快速压缩大型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆