First step: Create a Makezip class file
<?PHPclassmakezip{/** * Description: Main method: Generate Compressed Package * @author: MY * @param $dir _path want to compress directory: such as './demo/' * @param $zipName compression After the file name: such as './folder/demo.zip ' * @return string*/ functionZip$dir _path,$zipName) { $RELATIONARR=Array( $dir _path=Array( ' Originname ' =$dir _path, ' is_dir ' =true, ' children ' =Array() ) ); $key=Array_keys($RELATIONARR); $val=array_values($RELATIONARR); $this->modifiyfilename ($dir _path,$RELATIONARR[$dir _path[' Children ']); $zip=Newziparchive (); //Ziparchive::create is not created $zip->open ($zipName, Ziparchive::CREATE); $this->zipdir ($key[0], ",$zip,$val[0] [' Children ']); $zip-Close (); $this->restorefilename ($key[0],$val[0] [' Children ']); return true; } functionZipdir ($real _path,$zip _path, &$zip,$RELATIONARR) { $sub _zip_path=Empty($zip _path) ? ‘‘ :$zip _path. ‘\\‘; if(Is_dir($real _path)) { foreach($RELATIONARR as $k=$v) { if($v[' Is_dir ']) {//is a folder $zip->addemptydir ($sub _zip_path.$v[' Originname ']); $this->zipdir ($real _path. ‘\\‘ .$k,$sub _zip_path.$v[' Originname '],$zip,$v[' Children ']); } Else{//not a folder $zip->addfile ($real _path. ‘\\‘ .$k,$sub _zip_path.$k); $zip->deletename ($sub _zip_path.$v[' Originname ']); $zip->renamename ($sub _zip_path.$k,$sub _zip_path.$v[' Originname ']); } } } } functionModifiyfilename ($path, &$RELATIONARR) { if(!Is_dir($path) || !Is_array($RELATIONARR)) { return false; } if($DH=Opendir($path)) { $count= 0; while(($file=Readdir($DH)) !==false) { if(In_array($file,Array(‘.‘, ‘..‘,NULL)))Continue;//invalid file, redo if(Is_dir($path. ‘\\‘ .$file)) { $newName=MD5(Rand(0, 99999).Rand(0, 99999).Rand(0, 99999).Microtime() . ' dir '.$count); $RELATIONARR[$newName] =Array( ' Originname ' =Iconv(' GBK ', ' UTF-8 ',$file), ' is_dir ' =true, ' children ' =Array() ); Rename($path. ‘\\‘ .$file,$path. ‘\\‘ .$newName); $this->modifiyfilename ($path. ‘\\‘ .$newName,$RELATIONARR[$newName[' Children ']); $count++; } Else { $extension=STRCHR($file, ‘.‘); $newName=MD5(Rand(0, 99999).Rand(0, 99999).Rand(0, 99999).Microtime() . ' File '.$count); $RELATIONARR[$newName.$extension] =Array( ' Originname ' =Iconv(' GBK ', ' UTF-8 ',$file), ' is_dir ' =false, ' children ' =Array() ); Rename($path. ‘\\‘ .$file,$path. ‘\\‘ .$newName.$extension); $count++; } } } } functionRestorefilename ($path,$RELATIONARR) { foreach($RELATIONARR as $k=$v) { if(!Empty($v[' Children '])) { $this->restorefilename ($path. ‘\\‘ .$k,$v[' Children ']); Rename($path. ‘\\‘ .$k,Iconv(' UTF-8 ', ' GBK ',$path. ‘\\‘ .$v[' Originname '])); } Else { Rename($path. ‘\\‘ .$k,Iconv(' UTF-8 ', ' GBK ',$path. ‘\\‘ .$v[' Originname '])); } } }}
Step Two: Call the index.php file
<?PHP/** * The following code is implemented to compress the demo directory into a zip package and store it in the folder directory with the name Demo.zip*/include_once"./makezip.class.php";$makeZip=Newmakezip ();Try{ //repeated compression, it will automatically overwrite $res=$makeZip->zip ('./demo/', './folder/demo.zip ')); if(!$res){ Throw New Exception(' Compression failed '); } Echo' Success ';}Catch(Exception $e){ Echo $e-getMessage ();}
|--SupplementMakezipclass file is instantiated in theZiparchive class requiresOpen PHP support ziparchive in the php.ini file will Extension=php_zip.dll start;
PHP Implementation Compressed Directory Zip