2012-04-30 115 views
3

我需要一個將文件數組寫入一個zip文件的簡單函數。我從一個在線教程中找到了一些代碼並稍微修改了一些代碼,但似乎無法使其工作。它創建的zip文件,但是當我嘗試並提取它,我得到一個錯誤:ZipArchive文件無效

Windows cannot complete the extraction. 
The Compressed (zipped) Folder '...' is invalid. 

這裏是我的工作代碼:

public 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 'file exists'; } 

     $valid_files = array(); 
     if(is_array($files)) { 
     //cycle through each file 
     foreach($files as $file) { 
      //make sure the file exists 
      if(file_exists($file)) { 
      $valid_files[] = $file; 
      } 
     } 
     } 
     Zend_Debug::dump($valid_files); 

     if(count($valid_files)) { 
     //create the archive 
     $zip = new ZipArchive(); 
     if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) { 
      return 'could not open zip: '.$destination; 
     } 
     //add the files 
     foreach($valid_files as $file) { 
      $zip->addFile($file); 
     } 
     //debug 
     Zend_Debug::dump($zip->numFiles); 
     Zend_Debug::dump($zip->status); 

     $zip->close(); 

     //check to make sure the file exists 
     return file_exists($destination); 
     } else { 
     return 'no valid failes'. count($valid_files); 
     } 
} 

調試語句打印出以下幾點:

For $valid_files - array of one file name (full path to file) 
For $zip->numFiles - 1 
For $zip->status - 0 
The function returns true. 

關於我在做什麼的錯誤?

回答