tags:

views:

176

answers:

3

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)?

A: 

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?

DBruns
+1  A: 

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 ....

philip
Note: I updated the official docs with an example like this, and asked the extension maintainer for clarification.
philip
@philip: There seem to be more issues with deleteName(): It returns True if you try to delete a non-empty directory, but does not delete it. Btw. locateName() for directories also just works with trailing slashes.
desolat
fwiw, desolat and I contributed towards: http://bugs.php.net/51857
philip
A: 

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!

Pierre
Already got that bug: http://bugs.php.net/bug.php?id=51857
desolat