在我的本地系统上,PHP 的内置 zip 库能够在大约 800 毫秒内将 10 个文件 24MB zip 合并为 21 个文件 51MB zip,这与您报告的 200 毫秒/文件相当,但我不确定您的文件有多大或者您使用的是什么类型的硬件。
与您的指南作者最初使用的 Java 库不同,PHP 的 zip 库是用 C 实现的,因此您不会看到作者看到的相同的 Java 到 C 的性能提升。话虽如此,我不知道 Chillkat 的QuickAppend
工作原理或它与 PHP 的 zip 库的比较,但无论您使用 PHP 还是 Chillkat 都附加到预压缩文件似乎是最快的解决方案。
$destination = new ZipArchive;
$source = new ZipArchive;
if($source->open('a.zip') === TRUE
&& $destination->open('b.zip') === TRUE) {
$time_start = microtime(true);
$temp_dir = "/tmp/zip_" . time();
mkdir($temp_dir,0777,true);
$source->extractTo($temp_dir);
$source->close();
$files = scandir($temp_dir);
$file_count = 0;
foreach($files as $file) {
if($file == '.' || $file == '..')
continue;
$destination->addFile("$temp_dir/$file");
++$file_count;
}
$destination->close();
exec("rm -rf $temp_dir &");
$time_end = microtime(true);
$time = $time_end - $time_start;
print "Added $file_count files in " . ($time * 1000). "ms \n";
}
输出
-rw-rw-r-- 1 fuzzytree fuzzytree 24020997 Jun 4 15:57 a.zip
-rw-rw-r-- 1 fuzzytree fuzzytree 51418980 Jun 4 15:57 b.zip
fuzzytree@atlas:~/testzip$ php zip.php
Added 10 files in 872.43795394897ms
fuzzytree@atlas:~/testzip$ ls -ltr *zip
-rw-rw-r-- 1 fuzzytree fuzzytree 24020997 Jun 4 15:57 a.zip
-rw-rw-r-- 1 fuzzytree fuzzytree 75443030 Jun 4 15:57 b.zip