2 回答

TA贡献1834条经验 获得超8个赞
你的尝试copy()是正确的。与ZipArchive::extractTo()(提取并在目标中创建子文件夹)不同,该方法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条经验 获得超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 关注
- 150 浏览
添加回答
举报