1

I'm using the ZipArchive class in PHP for the first time, and I'm having a bit of trouble. All I'm trying to do is iterate through the files in the ZIP archive, and it all works, except it doesn't seem there's any way to get subdirectory names inside the ZIP file, and I need that information. If there are files inside the subdirectoy, I suppose I could parse the directory names out of the file names, but if there are empty directories, they're completely lost.

For instance, if I have this directory tree in a ZIP:

root_folder
root_folder -> test_file.ext
root_folder -> empty_dir

Now I try to read that ZIP file's entries into memory like this in PHP:

<?php
    $zip = new ZipArchive;
    if ($zip->open('test.zip') == TRUE) {
        for ($i = 0; $i < $zip->numFiles; $i++) {
            $filename = $zip->getNameIndex($i);
            echo $filename."<br />";
        }
        $zip->close();
    }
?>

If I do this, then root_folder\test_file.ext is found correctly, but root_folder\empty_dir is just lost entirely.

So how do I use the ZipArchive class to find all the files and directories, including empty ones inside the archive?

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • 1
    Works fine for me. Are you sure the zip file contains what you say it does? – Jonathan Apr 02 '17 at 04:34
  • ...wow. So today, I learned that Windows' default "send to zip" option will completely remove empty directories in the archive. Something I never knew, and now I feel stupid for not even thinking to check that. Thank you! – IceMetalPunk Apr 02 '17 at 04:46

0 Answers0