In specific Web site programming projects, sometimes you need to use PHP script code to control the compression and decompression zip file. The following is a simple PHP zip decompression implementation code, the need for friends can refer to the next careful study of a bit.
After PHP installation comes with a zip extension, first we need to open it, php.ini in the Extension=php_zip.dll front of the semicolon removed, and then restart the Web server.
PHP's Zip Decompression implementation code is 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 (' Test.zip ', './');
?>
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.