简体   繁体   English

使用PHP从ZIP文件中删除空目录

[英]Remove an empty directory from a ZIP file with PHP

PHP brings a class for ZIP file manipulation . PHP 为ZIP文件操作带来了一个 It also allows the creation of directories with addEmptyDir() and the deletion of an entry with deleteName(). 它还允许使用addEmptyDir()创建目录并删除带有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)? 有没有办法删除ZIP文件中的空文件夹(首选是buildin PHP功能)?

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 .... 所以,testDir /对testDir ....

You could always just extract it to a tmp directory, delete any empty directories with rmdir() and then zip it back up. 您可以随时将其解压缩到tmp目录,使用rmdir()删除所有空目录,然后将其压缩回来。

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. 例如:foo / a.txt和foo / b.txt是两个条目,但没有foo /目录。 However, it is possible to create an entry called foo/ to "emulate" a directory. 但是,可以创建一个名为foo /的条目来“模拟”目录。

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. 删除方法返回true而没有删除任何东西看起来像一个bug,我让菲利普在http://bugs.php.net上打开一个新的bug,所以我可以很快修复它。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM