2012-04-30 2 views
3

Мне нужна простая функция, которая записывает массив файлов в один 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. 

Любые идеи о том, что я делаю неправильно?

ответ

Смежные вопросы