Php implements zip compression file decompression code sharing (easy to understand)

Source: Internet
Author: User

I carefully studied the efficiency of the decompression program written in php, which is much higher than I thought. Since it is so good, I just need to optimize it and use it in my background, although the control panel in most of the space currently has the compression and decompression function, it is sometimes troublesome.

Before doing this, I have never touched on php compression. I have searched some websites on the Internet. Most of them are php compression classes and compression functions. A few hundred lines are less, and thousands of lines of code are more. This is hard for new users like me to understand, and I don't need such complex functions. Finally, refer to the function manual and understand several related functions.

Remember to enable zip and remove the semicolon before extension = php_zip.dll in php. ini.

Source code example:

Copy codeThe Code is as follows:
<? Php

// Configure php_zip.dll
// Phpinfo ();
Header ("Content-type: text/html; charset = UTF-8 ");

Function get_zip_originalsize ($ filename, $ path ){
// First determine whether the file to be decompressed exists
If (! File_exists ($ filename )){
Die ("File $ filename does not exist! ");
}
$ Starttime = explode ('', microtime (); // start time of decompression

// Convert the file name and path to the default gb2312 encoding in windows. Otherwise, the file name and path cannot be read.
$ Filename = iconv ("UTF-8", "gb2312", $ filename );
$ Path = iconv ("UTF-8", "gb2312", $ path );
// Open the compressed package
$ Resource = zip_open ($ filename );
$ I = 1;
// Traverse and read files in the compressed package
While ($ dir_resource = zip_read ($ resource )){
// Continue if it can be enabled
If (zip_entry_open ($ resource, $ dir_resource )){
// Obtain the name of the current project, that is, the file name corresponding to the package.
$ File_name = paipath.zip _ entry_name ($ dir_resource );
// Use the last slash (/) to split the path and use a string to extract the path.
$ File_path = substr ($ file_name, 0, strrpos ($ file_name ,"/"));
// If the path does not exist, a directory is created. "true" indicates that a multi-level directory can be created.
If (! Is_dir ($ file_path )){
Mkdir ($ file_path, 0777, true );
}
// If it is not a directory, write the file
If (! Is_dir ($ file_name )){
// Read this file
$ File_size = zip_entry_filesize ($ dir_resource );
// Read a maximum of 6 MB. If the file is too large, skip decompression and continue to the next step.
If ($ file_size <(1024*1024*6 )){
$ File_content = zip_entry_read ($ dir_resource, $ file_size );
File_put_contents ($ file_name, $ file_content );
} Else {
Echo "<p> ". $ I ++. "This file has been skipped. cause: the file is too large.-> ". iconv ("gb2312", "UTF-8", $ file_name ). "</p> ";
}
}
// Close the current
Zip_entry_close ($ dir_resource );
}
}
// Close the compressed package
Zip_close ($ resource );
$ Endtime = explode ('', microtime (); // extract End Time
$ Thistime = $ endtime [0] + $ endtime [1]-($ starttime [0] + $ starttime [1]);
$ Thistime = round ($ thistime, 3); // retain 3 as a decimal number
Echo "<p> decompressed !, This decompression takes $ thistime seconds. </P> ";
}

$ Size = get_zip_originalsize('331101.zip ', 'temp /');

?>

It took 300 seconds to decompress a small file with a size of more than 0.115 KB. It took more than 20 seconds to decompress a file with a size of over 30 MB (webpage files and small files.

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.