在php中删除zip中的文件夹

在php中删除zip中的文件夹,php,zip,Php,Zip,我有一个ZIP文件(skins.ZIP),具有以下结构: yellow |_resources |_theme |_codes 我需要删除skins.zip中名为theme/的文件夹。我尝试了以下代码,但没有成功 $zip=新的ZipArchive; 如果($zip->open('skins.zip')==TRUE){ $zip->deleteName('yellow/theme/'); $zip->close(); } 有人能帮我吗?试试看 $zip->del

我有一个ZIP文件(
skins.ZIP
),具有以下结构:

yellow  
  |_resources  
  |_theme  
  |_codes
我需要删除
skins.zip中名为
theme/
的文件夹。我尝试了以下代码,但没有成功

$zip=新的ZipArchive;
如果($zip->open('skins.zip')==TRUE){
$zip->deleteName('yellow/theme/');
$zip->close();
}
有人能帮我吗?

试试看

$zip->deleteName('./yellow/theme/');

我只编写了以下代码,并将
打印\r
输出留给您,让您了解发生了什么:

$z=新的ZipArchive;
$folder_to_delete=“gifreizer/resized/”//要相对于根目录删除的文件夹
如果($z->open(“gifresizer.zip”)==TRUE){//zip文件名
印刷费($z);
对于($i=0;$inumFiles;$i++){
$entry_info=$z->statidex($i);
打印(输入信息);
if(substr($entry_info[“name”]、0、strlen($folder_to_delete))=$folder_to_delete){
$z->deleteIndex($i);
}
}
}
它输出如下内容:

ZipArchive Object
(
    [status] => 0
    [statusSys] => 0
    [numFiles] => 10
    [filename] => C:\xampp\htdocs\test\zipdelete\gifresizer.zip
    [comment] => 
)
Array
(
    [name] => gifresizer/
    [index] => 0
    [crc] => 0
    [size] => 0
    [mtime] => 1339360746
    [comp_size] => 0
    [comp_method] => 0
)
Array
(
    [name] => gifresizer/frames/
    [index] => 1
    [crc] => 0
    [size] => 0
    [mtime] => 1328810540
    [comp_size] => 0
    [comp_method] => 0
)
Array
(
    [name] => gifresizer/gifresizer.php
    [index] => 2
    [crc] => 1967518989
    [size] => 18785
    [mtime] => 1328810430
    [comp_size] => 3981
    [comp_method] => 8
)

etc..
以下是我使用的方法(基于Taha Paksu的答案),带有php8味道:

/**
 * Removes an entry from an existing zip file.
 *
 * The name is the relative path of the entry to remove (relative to the zip's root).
 *
 *
 * @param string $zipFile
 * @param string $name
 * @param bool $isDir
 * @throws \Exception
 */
public static function deleteFromZip(string $zipFile, string $name, bool $isDir)
{
    $zip = new \ZipArchive();
    if (true !== ($res = $zip->open($zipFile))) {
        throw new \RuntimeException("Could not open the zipFile: " . $zip->getStatusString());
    }


    $name = rtrim($name, DIRECTORY_SEPARATOR);
    if (true === $isDir) {
        $name .= DIRECTORY_SEPARATOR;
    }
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $info = $zip->statIndex($i);
        if (true === str_starts_with($info['name'], $name)) {
            $zip->deleteIndex($i);
        }
    }
    $zip->close();
}
/**
*从现有zip文件中删除条目。
*
*名称是要删除的条目的相对路径(相对于zip的根)。
*
*
*@param string$zipFile
*@param string$name
*@param bool$isDir
*@throws\Exception
*/
公共静态函数deleteFromZip(字符串$zipFile,字符串$name,bool$isDir)
{
$zip=new\ZipArchive();
if(true!==($res=$zip->open($zipFile))){
抛出new\RuntimeException(“无法打开zipFile:.$zip->getStatusString());
}
$name=rtrim($name,目录\分隔符);
如果(真===$isDir){
$name.=目录\分隔符;
}
对于($i=0;$i<$zip->numFiles;$i++){
$info=$zip->statIndex($i);
if(true==str_以($info['name',$name])开头){
$zip->deleteIndex($i);
}
}
$zip->close();
}

到底出了什么问题?您是否启用了错误报告来调查可能的问题?它没有显示任何内容,并且在代码执行后文件夹仍然存在。$x=$zip->deleteName('yellow/theme/');var_dump(x美元);至少您知道,然后尝试使用递归删除?如果文件夹中有内容,它可能不允许删除文件夹?php.net的手册中没有太多关于它的内容,我以前也没有使用过它。猜猜看。@tpaksu:它会删除空目录,非空目录有问题。谢谢。。作品很棒(+1)。请注意,如果您想在删除不需要的索引后提取内容,则需要$z->close()存档并在提取之前重新打开它。否则,您提取的内容可能只是存档中所有剩余文件的部分提取。谢谢。是的,关闭对每一条流都很重要,因为您可能不会再次打开它。