简体   繁体   中英

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

I'm using php ziparchive function to open zip file and want to list all subfolders of a folder located in a zip file. For example... World.zip contains a folder country,foo & bar and I want to list names of all states folder of that country folder,but not of foo & bar

i found this note on the manuals which i got most of the code from which depends on looping through the total number of files

and i refactored it to match your needs and here is the code after refactoring

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM