删除php中的zip文件夹

时间:2012-06-10 20:06:03

标签: php zip

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

yellow  
  |_resources  
  |_theme  
  |_codes

我需要删除theme/中名为skins.zip的文件夹。我尝试过以下代码,但没有用。

$zip = new ZipArchive;
if ($zip->open('skins.zip') === TRUE) {
        $zip->deleteName('yellow/theme/');
        $zip->close();
}

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:6)

我只是以下代码并留下print_r输出,以便您了解正在发生的事情:

$z = new ZipArchive;
$folder_to_delete = "gifresizer/resized/";  //folder to delete relative to root
if($z->open("gifresizer.zip")===TRUE){      //zip file name
    print_r($z);
    for($i=0;$i<$z->numFiles;$i++){
        $entry_info = $z->statIndex($i);
        print_r($entry_info);
        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..

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是我使用的方法(基于 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();
}