PHP implementation zip compressed file decompression

Source: Internet
Author: User
Tags explode

The test uses two methods to achieve:

First: Need to turn on configuration Php_aip.dll

1<?PHP2 //need to turn on configuration Php_zip.dll3 //phpinfo ();4 Header("Content-type:text/html;charset=utf-8");5 6 functionGet_zip_originalsize ($filename,$path) {7   //determine if the file you want to unzip is present8   if(!file_exists($filename)){9      die(The file$filenamedoes not exist! ");Ten   } One   $starttime=Explode(‘ ‘,Microtime());//the time the decompression started A  - //Change the file name and path to the default gb2312 encoding of Windows system, otherwise you will not be read -   $filename=Iconv("Utf-8", "gb2312",$filename); the   $path=Iconv("Utf-8", "gb2312",$path); -   //opening a compressed package -   $resource= Zip_open ($filename); -   $i= 1; +   //iterate through the files in the compressed package -    while($dir _resource= Zip_read ($resource)) { +     //If you can open it, continue . A     if(Zip_entry_open ($resource,$dir _resource)) { at       //gets the name of the current project, which is the current file name in the compressed package -       $file _name=$path. Zip_entry_name ($dir _resource); -       //split the Last "/" and then use the string to intercept the path part -       $file _path=substr($file _name, 0,Strrpos($file _name, "/")); -       //if the path does not exist, a directory is created and True indicates that a multilevel directory can be created -       if(!Is_dir($file _path)){ in         mkdir($file _path, 0777,true); -       } to       //if it is not a directory, write the file +       if(!Is_dir($file _name)){ -         //Read this file the         $file _size= Zip_entry_filesize ($dir _resource); *         //Max Read 6M, if file is too large, skip unzip, continue next $         if($file _size< (1024*1024*30)){Panax Notoginseng           $file _content= Zip_entry_read ($dir _resource,$file _size); -           file_put_contents($file _name,$file _content); the}Else{ +           Echo"<p>".$i+ +. "This file has been skipped because: file is too large,".Iconv("gb2312", "Utf-8",$file _name). "</p>"; A         } the       } +       //Close the current -Zip_entry_close ($dir _resource); $     } $   } -   //To close a compressed package -Zip_close ($resource); the   $endtime=Explode(‘ ‘,Microtime());//time of Decompression end -   $thistime=$endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);Wuyi   $thistime=round($thistime, 3);//Keep 3 as decimal the   Echo"<p> decompression complete! , this decompression cost:$thistimeSeconds. </p> "; - } Wu  - $size= Get_zip_originalsize ('.. /.. /textaa.zip ', '. /.. /ffff/');

In the above method: $filename: File name, is the name of the file to extract, including the path relative to the method, $path means: Extract to (what directory, to/end)

The second method is to use the Pclzip class, the first method that is used in the project process because of the path problem

<?PHP/*php extracts files from zip compressed files*/$zip=Newziparchive;if($zip->open (' test.zip ') = = =TRUE) {//to use ANSI-encoded file formats for Chinese filenames  $zip->extractto (' foldername ');//extract all files//$zip->extractto ('/my/destination/dir/', Array (' pear_item.gif ', ' testfromfile.php '));//extract some files  $zip-Close (); Echo' OK ';} Else {  Echo' Failed ';}

PHP implementation zip compressed file decompression

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.