0

I am using this function to generate zip files:

function create_zip($files = array(),$destination = '',$overwrite = false) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
      $zip->addFile($file,$file);
      //echo $file;
    }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

    //close the zip -- done!
    $zip->close();

    //check to make sure the file exists
    echo $destination;
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}

I am using this to zip a vanilla, unaltered Wordpress install.(http://wordpress.org/) However for some reason the wp-content folder is having a 'ghost' empty file generated with it's name. The folder (and everything else) itself compresses correctly, but most unzip applications (including php's own extractTo()) break when they reach this unwanted file due to the name conflict.

I've looked into the folder/file structure and as far as I can see the only difference of note between this and the other folders in the site is that it is the biggest at 5.86 mb.

Can anyone suggest a fix / workaround?

tracer tong
  • 543
  • 6
  • 16
  • You could try the Zip function here: http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php – William Isted Mar 26 '12 at 12:55
  • Does the $files array contain only files or also directories? Maybe you want to add `is_file` to your `file_exists` check. And `$zip->addFile($file)` should be sufficient (leave the localname parameter). – initall Mar 26 '12 at 12:58
  • Is there something in error log? – piotrekkr Mar 26 '12 at 13:03
  • OK this problem is now solved after much messing about. @initall your condition seems to of been the fix in the end. – tracer tong Mar 26 '12 at 15:00

1 Answers1

0

This is the sample example to zip a folder.

        <?php
        function Create_zipArch($archive_name, $archive_folder)
        {
            $zip = new ZipArchive;
            if ($zip->open($archive_name, ZipArchive::CREATE) === TRUE)
            {
                $dir = preg_replace('/[\/]{2,}/', '/', $archive_folder . "/");

                $dirs = array($dir);
                while (count($dirs))
                {
                    $dir = current($dirs);
                    $zip->addEmptyDir($dir);

                    $dh = opendir($dir);
                    while ($file = readdir($dh))
                    {
                        if ($file != '.' && $file != '..')
                        {
                            if (is_file($file))
                                $zip->addFile($dir . $file, $dir . $file);
                            elseif (is_dir($file))
                                $dirs[] = $dir . $file . "/";
                        }
                    }
                    closedir($dh);
                    array_shift($dirs);
                }

                $zip->close();
                $result='success';
            }
            else
            {
                $result='failed';
            }
            return $result;
        }

        $zipArchName = "ZipFileName.zip"; // zip file name
        $FolderToZip = "zipthisfolder"; // folder to zip

        echo Create_zipArch($zipArchName, $FolderToZip);
        ?>
Hearaman
  • 8,466
  • 13
  • 41
  • 58