openstackweb/openstack/code/utils/Zipper.php
2014-10-31 16:59:18 -03:00

64 lines
1.9 KiB
PHP
Executable File

<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class Zipper{
function CreateZip($files = array(),$destination = '',$filename='',$overwrite = 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(!is_dir($destination)) mkdir($destination, 0775,true);
if($zip->open($destination.'/'.$filename,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,basename($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
return file_exists($destination);
}
else
{
return false;
}
}
public function getZipLink($files_to_zip = array(), $folder = '' ,$filename) {
if ($files_to_zip) {
$result = $this->CreateZip($files_to_zip, $folder,$filename,true);
return $result;
} else {
return false;
}
}
}