我正在使用 php ziparchive 函数打开 zip 文件,并希望列出位于 zip 文件中的文件夹的所有子文件夹。例如... World.zip 包含一个文件夹 country,foo & bar ,我想列出该国家文件夹的所有州文件夹的名称,但不是 foo & bar
问问题
2040 次
1 回答
0
我在手册上找到了这个注释,我从中获得了大部分代码,这些代码取决于循环文件的总数
我对其进行了重构以满足您的需求,这是重构后的代码
<?php
$filePath = 'zip/file.zip';
$za = new ZipArchive();
if ($za->open($filePath) !== true) { // check for the zip archive
echo "archive doesn't exist or it's on Read-only mode ";
} else {
$Tree = $pathArray = array(); //empty arrays
for ($i = 0; $i < $za->numFiles; $i++) {
$path = $za->getNameIndex($i);
$pathBySlash = array_values(explode('/', $path));
$c = count($pathBySlash);
$temp = &$Tree;
for ($j = 0; $j < $c - 1; $j++)
if (isset($temp[$pathBySlash[$j]]))
$temp = &$temp[$pathBySlash[$j]];
else {
$temp[$pathBySlash[$j]] = array();
$temp = &$temp[$pathBySlash[$j]];
}
if (substr($path, -1) == '/')
$temp[$pathBySlash[$c - 1]] = array();
else
$temp[] = $pathBySlash[$c - 1];
}
$array = $Tree['folder_name_to_list_its_files'];
// First style of Displaying
echo "<pre>";
print_r($array);
// Second style of Displaying
foreach ($array as $key => $value) {
foreach ($value as $val) {
echo $key . " | " . $val . "<br /> \n";
}
}
echo "</pre>";
}
于 2016-07-16T03:57:06.600 回答