简体   繁体   中英

Remove an empty directory from a ZIP file with PHP

PHP brings a class for ZIP file manipulation . It also allows the creation of directories with addEmptyDir() and the deletion of an entry with deleteName(). But the deletion does not work on directories (empty or not). Is there any way to delete empty folders in a ZIP file (prefered is buildin PHP functionality)?

You need to append a / to directory names. So, something like this works:

<?php
    $zip = new ZipArchive;
    if ($zip->open('test.zip') === TRUE) {
        $zip->deleteName('testDir/');
        $zip->close();
    }
?>

So, testDir/ versus testDir ....

You could always just extract it to a tmp directory, delete any empty directories with rmdir() and then zip it back up.

Another thing to check is permissions. Are you sure you have write permissions on the file you are trying to manipulate?

Little note about the notion of directories. There is not such thing as a directory. For example: foo/a.txt and foo/b.txt are two entries, but there is no foo/ directory. However, it is possible to create an entry called foo/ to "emulate" a directory.

The delete method returning true while nothing has been removed looks like a bug, I asked Philip to open a new bug at http://bugs.php.net so I can fix it soonish.

Thanks!

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