100 MB的文件 --> 10个ZIP调用(每次10 MB的zip) --> 1个ZIP文件
如果我要将一个100 MB的文件添加到Zip文件中(每次压缩10 MB),我需要发起10个调用。
问题在于,我们的系统有内存和时间限制(每次调用最多处理10到15MB)。
因此,使用多个调用对大文件进行压缩是基本思路。
如有需要,我可以提供更多数据。
100 MB的文件 --> 10个ZIP调用(每次10 MB的zip) --> 1个ZIP文件
如果我要将一个100 MB的文件添加到Zip文件中(每次压缩10 MB),我需要发起10个调用。
问题在于,我们的系统有内存和时间限制(每次调用最多处理10到15MB)。
因此,使用多个调用对大文件进行压缩是基本思路。
如有需要,我可以提供更多数据。
你曾经尝试过PECL Zip吗?
使用以下代码压缩两个文件,没有任何内存限制问题。时间限制可能会重置。我的环境:内存限制为3MB,最大执行时间为20秒。
<?php
set_time_limit(0);
$zip = new ZipArchive();
$zip->open('./test.zip', ZipArchive::CREATE);
$zip->addFile('./testa'); // 1.3 GB
$zip->addFile('./testb'); // 700mb
$zip->close();
注意: set_time_limit() 在php < 5.4并且save_mode=on的情况下无法使用。
try {
$t = new zip('/bin', '/tmp/test.zip');
$t->zip();
if ($t->waitForFinish(100))
echo "Succes :)";
else
echo $t->getOutput();
} catch ($e) {
echo $e->getMessage();
}
不必等到进程结束,您可以将pid写入数据库,如果进程已经完成,则提供文件...
<?php
$fh = fopen("largefile.txt", 'w');
fclose($fh);
$fh = fopen("largefile.txt", 'ab');
$size = 500;
while($size--) {
$l = chr(rand(97, 122));
fwrite($fh, str_repeat($l, 1024*1024));
}
fclose($fh);
?>
要使用zip类,我们需要执行以下操作:
<?php
include('zip.php');
$zip = new Zip();
$zip->setZipFile("largefile.zip");
//the firstname is the name as it will appear inside the zip and the second is the filelocation. In my case I used the same, but you could rename the file inside the zip easily.
$zip->addLargeFile("largefile.txt", "largefile.txt");
$zip->finalize();
?>
现在,在我的服务器上创建一个大的zip文件只需要几秒钟,结果是一个550KB的文件。
如果由于某种奇怪的原因,您仍然需要在多个网络请求中执行此操作,请告诉我。我仍然拥有最初用于执行此操作的代码。