2

I have a zip file uploaded to server for automated extract.

the zip file construction is like this:

/zip_file.zip/folder1/image1.jpg
/zip_file.zip/folder1/image2.jpg
/zip_file.zip/folder1/image3.jpg

Currently I have this function to extract all files that have extension of jpg:

$zip = new ZipArchive();
    if( $zip->open($file_path) ){
        $files = array();
        for( $i = 0; $i < $zip->numFiles; $i++){
            $entry = $zip->statIndex($i);
            // is it an image?  
            if( $entry['size'] > 0 && preg_match('#\.(jpg)$#i', $entry['name'] ) ){
                $f_extract = $zip->getNameIndex($i);
                $files[] = $f_extract;
            }
        }
        if ($zip->extractTo($dir_name, $files) === TRUE) {
        } else {
            return FALSE;
        }

        $zip->close();
    }

But by using the function extractTo, it will extract to myFolder as ff:

/myFolder/folder1/image1.jpg
/myFolder/folder1/image2.jpg
/myFolder/folder1/image3.jpg

Is there any way to extract the files in folder1 to the root of myFolder?

Ideal:

/myFolder/image1.jpg
/myFolder/image2.jpg
/myFolder/image3.jpg

PS: incase of conflict file name I only need to not extract or overwrite the file.

DucDigital
  • 4,580
  • 9
  • 49
  • 97

3 Answers3

4

Use this little code snippet instead. It removes the folder structure in front of the filename for each file so that the whole content of the archive is basically extracted to one folder.

<?php
$path = "zip_file.zip";

$zip = new ZipArchive();
if ($zip->open($path) === true) {
    for($i = 0; $i < $zip->numFiles; $i++) {
        $filename = $zip->getNameIndex($i);
        $fileinfo = pathinfo($filename);
        copy("zip://".$path."#".$filename, "/myDestFolder/".$fileinfo['basename']);
    }                  
    $zip->close();                  
}
?>
Lilly-R Brock
  • 205
  • 2
  • 6
Fabianius
  • 695
  • 6
  • 13
  • 6
    This solution will overwrite files with duplicate names in different subdirectories. – shelhamer Jul 13 '11 at 17:25
  • Thanks found that here http://php.net/manual/en/ziparchive.extractto.php#100802 but landed here when trying to understand where the resource is that shows what the `zip://` protocol is, anybody have a ref url for that? – blamb Dec 12 '17 at 18:03
0

Here: (i tried to manage everything)

$zip = new ZipArchive();
if( $zip->open($file_path) ){
    $files = array();
    for( $i = 0; $i < $zip->numFiles; $i++){
        $entry = $zip->statIndex($i);
        // is it an image?  
        if( $entry['size'] > 0 && preg_match('#\.(jpg)$#i', $entry['name'] ) ){
            $f_extract = $zip->getNameIndex($i);
            $files[] = $f_extract; /* you man want to keep this array (use it to show result or something else) */

            if ($zip->extractTo($dir_name, $f_extract) === TRUE) {
                $solid_name = basename($f_extract);
                if(strpos($f_extract, "/")) // make sure zipped file is in a directory
                {
                    if($dir_name{strlen($dir_name)-1} == "/") $dir_name = substr($dir_name, 0, strlen($dir_name)-1); // to prevent error if $dir_name have slash in end of it
                    if(!file_exists($dir_name."/".$solid_name)) // you said you don't want to replace existed file
                        copy($dir_name."/".$f_extract, $dir_name."/".$solid_name); // taking file back to where you need [$dir_name]
                    unlink($dir_name."/".$f_extract); // [removing old file]
                    rmdir(str_replace($solid_name, "", $dir_name."/".$f_extract)); // [removing directory of it]
                }
            } else {
                 echo("error on export<br />\n");
            }
        }
    }


    $zip->close();
}
0

You can do so by using the zip:// syntax instead of Zip::extractTo as described in the php manual on extractTo().

You have to match the image file name and then copy it:

if ($entry['size'] > 0 && preg_match('#\.(jpg)$#i', $entry['name'])) {
  copy('zip://' . $file_path . '#' . $entry['name'], '/root_dir/' . md5($entry['name']) . '.jpg');
}

The above replaces your for loop's if statement and makes your extractTo unnecessary. I used the md5 hash of the original filename to make a unique name. It is extremely unlikely you will have any issues with overwriting files, since hash collisions are rare. Note that this is a bit heavy duty, and instead you could do str_replace('/.', '', $entry['name']) to make a new, unique filename.

Full solution (modified version of your code):

<?php
$zip = new ZipArchive();
if ($zip->open($file_path)) {
  for ($i = 0; $i < $zip->numFiles; $i++) {
    $entry = $zip->statIndex($i);
    // is it an image?
    if ($entry['size'] > 0 && preg_match('#\.(jpg)$#i', $entry['name'])) {
      # use hash (more expensive, but can be useful depending on what you're doing
      $new_filename = md5($entry['name']) . '.jpg';
      # or str_replace for cheaper, potentially longer name:
      # $new_filename = str_replace('/.', '', $entry['name']);
      copy('zip://' . $file_path . '#' . $entry['name'], '/myFolder/' . $new_filename);
    }
  }
  $zip->close();
}
?>
shelhamer
  • 29,752
  • 2
  • 30
  • 33
  • It's definitely the better solution to check for duplicates. However, why use a hash algorithm to make file names unique? Hashing each file takes a lot of time in case there are many files in the ZIP archive. I guess a solution that preserves the original filename is preferred, anyway. You could for instance append a consecutive number in brackets to files with the same filename. – Fabianius Jul 13 '11 at 17:38
  • Consecutive number could potentially create duplicates itself, if someone has named files with consecutive numbers at the end :). However, you are right that hashing can be overkill so I have provided an alternative with turning the path into a filename with `str_replace` – shelhamer Jul 13 '11 at 17:44