I need a simple function that writes an array of files to a single zip file. I found the code from the online tutorial and changed it a bit, but I can't get it to work. It creates a zip file, but when I try to extract it, I get an error message:
Windows cannot complete the extraction.
The Compressed (zipped) Folder '...' is invalid.
Here is the code I'm working with:
public function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return 'file exists'; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
Zend_Debug::dump($valid_files);
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
return 'could not open zip: '.$destination;
}
foreach($valid_files as $file) {
$zip->addFile($file);
}
Zend_Debug::dump($zip->numFiles);
Zend_Debug::dump($zip->status);
$zip->close();
return file_exists($destination);
} else {
return 'no valid failes'. count($valid_files);
}
}
Debug statements print the following:
For $valid_files - array of one file name (full path to file)
For $zip->numFiles - 1
For $zip->status - 0
The function returns true.
Any ideas on what I'm doing wrong?
source
share