/* Creates a compressed zip file */ function create_zip ($files = Array (), $destination = ', $overwrite = False) { // If the zip file already exists and overwrite are 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) { //make throaty the file exists if (file_exists ($file)) { $valid _files[] = $file; } }
//if we have ... 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) { $zip->addfile ($file, $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 throaty the file exists return file_exists ($destination); } Else { return false; } } /***** Example Usage ***/ $files =array (' file1.jpg ', ' file2.jpg ', ' file3.gif '); Create_zip ($files, ' Myzipfile.zip ', true);
PHP uncompressed Zip file
/********************** * @file-path to zip file * @destination-destination directory for unzipped files */ function Unzip_file ($file, $destination) { Create Object $zip = new Ziparchive (); Open Archive if ($zip->open ($file)!== TRUE) { Die (' could not open archive '); } Extract contents to destination directory $zip->extractto ($destination); Close Archive $zip->close (); Echo ' Archive extracted to directory '; }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.