Compress a file
We generate a compressed package from a file.
<? php $path = "C:/wamp/www/log.txt" ; $filename = "Test.zip" ; $zip = new Ziparchive (); $zip ->open ( $filename , ziparchive:: CREATE); // Open the Compact package $zip ->addfile ( $path , basename ( $path )); // $zip ->close (); // Close the package
The code above generates a test.zip of c:/wamp/www/log.txt file compression and is saved in the current directory.
Compress multiple Files
Compressing multiple files, in fact, is addfile executed several times, can be through the array of traversal to achieve
<?PHP$fileList=Array( "C:/wamp/www/log.txt", "c:/wamp/www/weixin.class.php");$filename= "Test.zip";$zip=Newziparchive ();$zip->open ($filename, ziparchive::create);//opening a compressed packageforeach($fileList as $file){ $zip->addfile ($file,basename($file));//to add a file to a compressed package}$zip->close ();//To close a compressed package
Compress a Directory
<?PHPfunctionAddfiletozip ($path,$zip){ $handler=Opendir($path);//opens the current folder specified by $path. while(($filename=Readdir($handler))!==false){ if($filename! = "." &&$filename!= "..") {//folder file name is '. ' and '. ', do not operate on them if(Is_dir($path." /".$filename)){//if an object that is read is a folder, the recursiveAddfiletozip ($path." /".$filename,$zip); }Else{//adding files to a Zip object $zip->addfile ($path." /".$filename); } } } @Closedir($path);}$zip=Newziparchive ();if($zip->open (' Rsa.zip ', ziparchive::overwrite) = = =TRUE) {Addfiletozip (' rsa/',$zip);//invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method $zip->close ();//close the processed zip file}
Compress and download zip packages
When I do, we need to pack, provide the download, and then delete the compressed package.
Can be divided into the following steps:
- Determine the path that is given, whether it is a folder or a file. The folder also needs to traverse the add file.
- Set the relevant file header and use the
readfile
function to provide the download.
- Use
unlink
function to delete a compressed package<?PHPfunctionAddfiletozip ($path,$zip){ $handler=Opendir($path);//opens the current folder specified by $path. while(($filename=Readdir($handler))!==false){ if($filename! = "." &&$filename!= "..") {//folder file name is '. ' and '. ', do not operate on them if(Is_dir($path." /".$filename)){//if an object that is read is a folder, the recursiveAddfiletozip ($path." /".$filename,$zip); }Else{//adding files to a Zip object $zip->addfile ($path." /".$filename); } } } @Closedir($path);}$zip=Newziparchive ();if($zip->open (' Rsa.zip ', ziparchive::overwrite) = = =TRUE){ $path= ' rsa/'; if(Is_dir($path)){//give a folder, package a folderAddfiletozip ($path,$zip); }Else if(Is_array($path)){//To give the file path in the form of an array foreach($path as $file){ $zip->addfile ($file); } }Else{//give only one file $zip->addfile ($path); } $zip->close ();//close the processed zip file}
PHP generates ZIP compression package