如何在不解压缩的情况下列出 zip 文件中文件夹的子文件夹?

How to list subfolders of a folder inside zip file without extracting?

我正在使用 php ziparchive 函数打开 zip 文件,并希望列出位于 zip 文件中的文件夹的所有子文件夹。 例如... World.zip 包含一个文件夹 country,foo & bar 我想列出该国家文件夹的所有状态文件夹的名称,但不是 foo & bar

我在手册中找到了这个 note 我从中获得了大部分代码 这取决于遍历文件总数

我重构了它以满足您的需求,这是重构后的代码

<?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>";
}