PHP ZipArchive list archive contents

I have wp.zip and would like to list only one level of files/folders. My current code:

$zip = new \ZipArchive();
$zip->open('wp.zip'), \ZipArchive::RDONLY);

for ($i = 0; $i < $zip->numFiles; $i++) {
 $stat = $zip->statIndex($i);
 echo $stat['name'] . ' ';
}

This code spits out entire list of files recursively.

I need only first level, like this:

wp-admin/
wp-content/
index.php
config.php
<...>

What's the best approach to achieve my goal?

Tagged:

Comments

  • Shit, NVM. Seems Zip has no levels. I will just parse the damn list and format it.

  • Another ticket resolved by our helpful LESbians.

  • edited January 2023

    @shallow said:
    Another ticket resolved by our helpful LESbians.

    Here is solution:

    $zip = new \ZipArchive();
    $zip->open('wp.zip');
    
    function getEntryList($zip, $level = 0){
        $res = [];
        for($i = 0; $i < $zip->numFiles; ++$i){
            $name = explode("/", trim($zip->statIndex($i)['name'],"/"));
            if(count($name) == $level + 1){
                $res[] = end($name);
            }
        }
        return $res;
    }
    
    print_r(getEntryList($zip));
    
    Thanked by (2)shallow someTom
  • @legendary said: name = explode

    BOOM goes the dynamite.

Sign In or Register to comment.