Converting Multiple files to zip and saving them in ownCloud
- by user1055380
I wanted to convert an array with some css, js and html files into a zip file and save them in ownCloud (it has it's own framework but it's knowledge is not required.) What I am saving is an infinite loop of zip files, as in, a zip inside a zip so I can't even check that the code is working correctly or not. Please help.
Here is the link to the code.
<?php
/* creates a compressed zip file */
$filename = $_GET["filename"];
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 => $local) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[$file] = $local;
}
}
}
//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 => $local) {
$zip->addFile($file, $local);
}
//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
return file_exists($destination);
}
else
{
return false;
}
}
$files_to_zip = array(
'apps/impressionist/css/mappingstyle.css' => '/css/mappingstyle.css',
'apps/impressionist/css/style.css' => '/css/style.css',
'apps/impressionist/js/jquery.js' => '/scripts/jquery.js',
'apps/impressionist/js/impress.js' => '/scripts/impress.js',
realpath('apps/impressionist/output/'.$filename.'.html') => $filename.'.html'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip, $filename.'.zip');
$save_file = OC_App::getStorage('impressionist');
$save_file ->file_put_contents($filename.'.zip',$files_to_zip);
?>