PHP implementation Zip Compressed file extract code sharing (easy to understand) _php tutorial

Source: Internet
Author: User
Careful study of the original use of PHP to write the decompression program efficiency than the imagination is still much higher, since so good, simply optimize again after the use of their own backstage, although now most of the space Control Panel has compression and decompression this function, but after all, sometimes some trouble.

Before doing this, did not contact PHP compression this piece, online search some, most are PHP compression class, compression function, less then hundreds of lines, more than thousands of lines of code. This is very difficult for me to be a novice, and I do not need such a complex function. Finally, referring to the function manual, after figuring out a few related functions, we understand how to go about the whole.

Remember to open the zip and remove the semicolon in front of the Extension=php_zip.dll in the php.ini.

Source code Example:

Copy the Code code as follows:

Need to turn on configuration Php_zip.dll
Phpinfo ();
Header ("Content-type:text/html;charset=utf-8");

function Get_zip_originalsize ($filename, $path) {
Determine if the file you want to unzip is present
if (!file_exists ($filename)) {
Die ("file $filename does not exist! ");
}
$starttime = Explode (' ', Microtime ()); The time the decompression started

Turn the file name and path to the default gb2312 encoding for Windows system, otherwise you will not be reading
$filename = Iconv ("Utf-8", "gb2312", $filename);
$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.
if (Zip_entry_open ($resource, $dir _resource)) {
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)) {
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);
Max Read 6M, if file is too large, skip unzip, continue next
if ($file _size< (1024*1024*6)) {
$file _content = Zip_entry_read ($dir _resource, $file _size);
File_put_contents ($file _name, $file _content);
}else{
echo "

". $i + +." This file has been skipped because the file is too large, ". Iconv (" gb2312 "," Utf-8 ", $file _name)."

";
}
}
Close the current
Zip_entry_close ($dir _resource);
}
}
To close a compressed package
Zip_close ($resource);
$endtime = Explode (' ', Microtime ()); Time of Decompression End
$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);
$thistime = Round ($thistime, 3); Keep 3 as Decimal
echo "

Decompression Complete! , this decompression cost: $thistime seconds.

";
}

$size = get_zip_originalsize (' 20131101.zip ', ' temp/');

?>

The test extracted a small file of more than 300 KB, took 0.115 seconds, the test extracted a more than 30 MB (web files, small files more), took more than 20 seconds.

http://www.bkjia.com/PHPjc/768138.html www.bkjia.com true http://www.bkjia.com/PHPjc/768138.html techarticle careful study of the original use of PHP to write the decompression program efficiency than the imagination is still much higher, since so good, simply optimize again after the use of their backstage, although now most ...

  • 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.