Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a folder inside zip in php

Tags:

php

zip

I've a ZIP file (skins.zip) having the following structure:

yellow  
  |_resources  
  |_theme  
  |_codes

I need to delete folder called theme/ inside the skins.zip. I've tried the following code but didn't worked.

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

Can anyone help me, please?

like image 310
vkGunasekaran Avatar asked Jun 10 '12 20:06

vkGunasekaran


1 Answers

I've just the following code and left the print_r output for you to understand what's going on:

$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);
        }
    }
}

It outputs something like this:

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..
like image 161
Taha Paksu Avatar answered Sep 25 '22 19:09

Taha Paksu