retrieve folders names only from a zip file with php?


I'm trying to retrieve folders name from a zip file. I wrote this simple function :

<?php class zipadmin{ private $filename ; private $folder ; public function __construct($filename,$folder){ $this->zip = new ZipArchive; $this->file = $filename ; $this->folder = $folder ; } public function listzip(){ if ($this->zip->open($this->file) == TRUE) { $info = $this->zip->statIndex(0); $output = str_replace('/','',$info['name']); return $output; } } }

Problem that if the zip folder contain other files which is not included inside the folders it return all the files names. I need it to return folders names only and discard any files name.


You can try to check when $info['crc'] equals to zero.

class zipadmin{ private $file; private $folder; private $zip; public function __construct($filename, $folder) { $this->zip = new ZipArchive; $this->file = $filename ; $this->folder = $folder ; } public function listzip() { $res = false; if ($this->zip->open($this->folder . $this->file) == TRUE) { $i = 0; while ($info = $this->zip->statIndex($i)) { if ($info['crc'] == 0 && preg_match('#^[^/]*?/$#', $info['name'])) $res[] = preg_replace('#^([^/]*?)/$#', '$1', $info['name']); $i++; } } return $res; } }

<strong>Usage example:</strong>

$z = new zipadmin('test.zip', './'); // test.zip in my example is in same folder print_r($z->listzip());

<strong>Output</strong> (array of root-directories only):

Array ( [0] => folder1 [1] => folder2 [2] => folder3 [3] => folder4 )

In my test.zip archive I have few files in root directory of archive and 4 directories folder1, folder2, folder3 and folder4with some files and sub-directories inside them. Running method against archive with no folders returns boolean <strong>false</strong>.


<ul><li>Fixed regex pattern to match everything before first slash /.</li> </ul>


  • php ZipArchive count number of files inside archive
  • Zip inside zip (php)
  • Attaching a file from .Zip folder
  • D: how to extract data from archive?
  • How to access list of email accounts with cPanel API?
  • str_replace with line break? [closed]
  • How to get Confirmation of email send or discarded in android
  • Dom Node for PHP find href attribute issue
  • String encoding problem on PdoStatement->bindParam()?
  • drop duplicates pandas dataframe
  • dm-script catch error with “Analyze Particles”
  • Encode string to match encoded form field name in PHP POST array
  • Number of threads being used during Parallel.ForEach
  • New dataframe with difference between first and last values of repeated measurements?
  • Highcharts - Column chart with empty columns for date in x-axis
  • Access the state of control in Winforms from another application
  • RegistryKey.GetSubKeyNames returns names that are not in that subkey!
  • Get used tables from sql query [duplicate]
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • ggplot2: make the points on the line a darker color than the line color
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • How to access meteor package name inside package?
  • How do I remove all but some records based on a threshold?
  • Android changing fragment order inside FragmentPagerAdapter
  • Installed module is empty
  • How to rebase a series of branches?
  • Deserializing XML into class C#
  • Can I make an Android app that runs a web view in Chrome 39?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • python draw pie shapes with colour filled
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app