2 回答

TA貢獻1834條經(jīng)驗 獲得超8個贊
你的嘗試copy()是正確的。與ZipArchive::extractTo()(提取并在目標中創(chuàng)建子文件夾)不同,該方法copy()只是將指定文件從存檔復制/提取到目標。
這個例子應該工作:
$archive = "testarchive.zip";
$dest_dir = "./dest";
if (!is_dir($dest_dir)) {
if (!mkdir($dest_dir, 0755, true)) die("failed to make directory $dest_dir\n");
}
$zip = new ZipArchive;
if (!$zip->open($archive)) die("failed to open $archive");
for($i = 0; $i < $zip->numFiles; $i++) {
$file_name = $zip->getNameIndex($i);
$file_info = pathinfo($file_name);
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (preg_match('/pdf/i', $file_ext)) {
copy("zip://".$archive."#".$file_name, $dest_dir.'/'.$file_info['basename']);
}
}
$zip->close();
測試檔案結構:
xxxxx@xxxxxx:~/Documents$ tree testarchive
testarchive
└── test
└── blubb
└── test.pdf
然后將該文件夾testarchive壓縮為testarchive.zip.
運行上面的代碼后:
xxxxx@xxxxxx:~/Documents$ tree dest
dest
└── test.pdf

TA貢獻1788條經(jīng)驗 獲得超4個贊
您需要從 zip 存檔中提取。
for ($i = 0; $i < $zip->numFiles; ++$i) {
$path = $zip->getNameIndex($i);
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (preg_match('/pdf/i', $ext)) {
$dest_basename = pathinfo($path, PATHINFO_BASENAME);
echo $path, PHP_EOL;
file_put_contents("$dest/$dest_basename", $zip->getFromIndex($i))
}
}
- 2 回答
- 0 關注
- 149 瀏覽
添加回答
舉報