PHP generates ZIP compression package

Source: Internet
Author: User
Tags ziparchive

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:

  1. Determine the path that is given, whether it is a folder or a file. The folder also needs to traverse the add file.
  2. Set the relevant file header and use the readfile function to provide the download.
  3. Useunlinkfunction 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.