我在具有此结构的网站上有 compressed_file.zip :
我想从 version_1.x 文件夹中提取所有内容到我的根文件夹:
我该怎么做?没有递归就可能吗?
答案 0 :(得分:5)
这是可能的,但您必须使用ZipArchive::getStream
自己读取和写入文件:
$source = 'version_1.x';
$target = '/path/to/target';
$zip = new ZipArchive;
$zip->open('myzip.zip');
for($i=0; $i<$zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
// Skip files not in $source
if (strpos($name, "{$source}/") !== 0) continue;
// Determine output filename (removing the $source prefix)
$file = $target.'/'.substr($name, strlen($source)+1);
// Create the directories if necessary
$dir = dirname($file);
if (!is_dir($dir)) mkdir($dir, 0777, true);
// Read from Zip and write to disk
$fpr = $zip->getStream($name);
$fpw = fopen($file, 'w');
while ($data = fread($fpr, 1024)) {
fwrite($fpw, $data);
}
fclose($fpr);
fclose($fpw);
}
答案 1 :(得分:0)
查看extractTo
的文档。例1。
答案 2 :(得分:0)
使用@ netcoder的解决方案时,我遇到了与@quantme类似的错误。我对该解决方案进行了更改,并且没有任何错误。
$source = 'version_1.x';
$target = '/path/to/target';
$zip = new ZipArchive;
if($zip->open('myzip.zip') === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
// Skip files not in $source
if (strpos($name, "{$source}/") !== 0) continue;
// Determine output filename (removing the $source prefix)
$file = $target.'/'.substr($name, strlen($source)+1);
// Create the directories if necessary
$dir = dirname($file);
if (!is_dir($dir)) mkdir($dir, 0777, true);
// Read from Zip and write to disk
if($dir != $target) {
$fpr = $zip->getStream($name);
$fpw = fopen($file, 'w');
while ($data = fread($fpr, 1024)) {
fwrite($fpw, $data);
}
fclose($fpr);
fclose($fpw);
}
}
$zip->close();
}