PHP functions for compressing files into ZIP files
<? Php <br/>/* @ creates a compressed zip file: function for compressing multiple files into one ZIP file <br/> * @ $ files array-type instance array ("1.jpg ", "2.jpg"); <br/> * @ destination the path of the target file, for example," C: /androidyue.zip "<br/> * @ $ overwrite indicates whether to overwrite the same file as the target file. <br/> * @ recorded by androidyue <br/> * @ blog: http://thinkblog.sinaapp.com <br/> */<br/> function create_zip ($ files = array (), $ Destination = '', $ overwrite = false) {<br/> // If the ZIP file already exists and overwrit E is false, return false <br/> // If the ZIP file already exists and is set to not overwrite, false is returned. <br/> If (file_exists ($ destination )&&! $ Overwrite) {return false ;}< br/> // vars <br/> $ valid_files = array (); <br/> // If files were passed in... <br/> // get the real and valid file name <br/> If (is_array ($ files )) {<br/> // cycle through each file <br/> foreach ($ files as $ file) {<br/> // make sure the file exists <br/> If (file_exists ($ file) {<br/> $ valid_files [] = $ file; <br/>}< br/> // if we have good files... <br/> // if a valid file exists <br/> If (count ($ valid_f Iles) {<br/> // create the archive <br/> $ zip = new ziparchive (); <br/> // open the file and overwrite it if the file already exists, if not, create <br/> if ($ zip-> open ($ destination, $ overwrite? Ziparchive: overwrite: ziparchive: Create )! = True) {<br/> return false; <br/>}< br/> // Add the files <br/> // Add a file to the compressed file <br/> foreach ($ valid_files as $ file) {<br/> $ zip-> AddFile ($ file, $ file ); <br/>}< br/> // debug <br/> // echo the ZIP Archive ins INS, $ zip-> numfiles, 'files with a status ', $ zip-> status; <br/> // close the zip -- done! <Br/> // close the file <br/> $ zip-> close (); <br/> // check to make sure the file exists <br/> // check whether the file exists <br/> return file_exists ($ destination ); <br/>} else {<br/> // returns false if no valid file exists. <br/> return false; <br/>}< br/>/***** <br/> // test the function <br/> $ files = array ('temp. php', 'test. PHP '); <br/> create_zip ($ files, 'myzipfile.zip', true); <br/> *****/<br/>?>
From http://thinkblog.sinaapp.com Open Source