我正在尝试解压缩一个可能包含1500多个pdf文件的zip文件。压缩文件应分部分解压缩到一个文件夹中,以避免20mb的文件一次溢出服务器内存。
我已经找到了一个关于如何分段解压缩的例子。但是,此方法不会创建一个目录或可以查看未打包文件的内容。它只创建了一个文件,而不是一个目录,看起来又是一个新的zip。
$sfp = gzopen($srcName, "rb");
$fp = fopen($dstName, "w+");
while ($string = gzread($sfp, 4096)) {
fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);
如上所述,此函数创建一些看起来像其他zip文件的文件。如果我创建了要首先将其解压缩到的文件夹,并将其用作$dstName,则会发出找不到该文件的警告。此外,当我让它创建一个在目标链接末尾带有"/"的"文件"时,它会发出警告。
使用opendir而不是fopen不会发出警告,但似乎什么都没有提取出来,猜测处理程序的类型不对。
如何将这个大的压缩文件分部分解压缩到文件夹中?
(PK)Zip和GZip是两种完全不同的格式;gzopen
无法打开zip存档。
要打开PKZip档案,请查看PHP-Zip扩展。
<?php
function unzip($file) {
$zip = zip_open($file);
if (is_resource($zip)) {
$tree = "";
while (($zip_entry = zip_read($zip)) !== false) {
echo "Unpacking " . zip_entry_name($zip_entry) . "'n";
if (strpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR) !== false) {
$last = strrpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR);
$dir = substr(zip_entry_name($zip_entry), 0, $last);
$file = substr(zip_entry_name($zip_entry), strrpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR) + 1);
if (!is_dir($dir)) {
@mkdir($dir, 0755, true) or die("Unable to create $dir'n");
}
if (strlen(trim($file)) > 0) {
//Downloading in parts
$fileSize = zip_entry_filesize($zip_entry);
while ($fileSize > 0) {
$readSize = min($fileSize, 4096);
$fileSize -= $readSize;
$content = zip_entry_read($zip_entry, $readSize);
if ($content !== false) {
$return = @file_put_contents($dir . "/" . $file, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
if ($return === false) {
die("Unable to write file $dir/$file'n");
}
}
}
}
fclose($outFile);
} else {
file_put_contents($file, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
}
}
} else {
echo "Unable to open zip file'n";
}
}
unzip($_SERVER['DOCUMENT_ROOT'] . '/test/testing.zip');
?>