我正在尝试使用PHP解压缩14MB存档,代码如下:
$zip = zip_open("c:\kosmas.zip");
while ($zip_entry = zip_read($zip)) {
$fp = fopen("c:/unzip/import.xml", "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
break;
}
zip_close($zip);
}
Run Code Online (Sandbox Code Playgroud)
它在我的本地主机上失败,128MB内存限制与经典" Allowed memory size of blablabla bytes exhausted".在服务器上,我有16MB的限制,有没有更好的方法来做到这一点,以便我可以适应这个限制?我不明白为什么这需要分配超过128MB的内存.提前致谢.
解决方案: 我开始以10Kb块的形式读取文件,问题解决了峰值内存使用率为1.5MB.
$filename = 'c:\kosmas.zip';
$archive = zip_open($filename);
while($entry = zip_read($archive)){
$size = zip_entry_filesize($entry);
$name = zip_entry_name($entry);
$unzipped = fopen('c:/unzip/'.$name,'wb');
while($size > 0){
$chunkSize = ($size > 10240) ? 10240 : $size;
$size -= $chunkSize;
$chunk = zip_entry_read($entry, $chunkSize);
if($chunk !== false) fwrite($unzipped, $chunk);
}
fclose($unzipped);
}
Run Code Online (Sandbox Code Playgroud)
为什么要一次读取整个文件?
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
Run Code Online (Sandbox Code Playgroud)
尝试读取其中的小块并将它们写入文件。
| 归档时间: |
|
| 查看次数: |
6216 次 |
| 最近记录: |