Extract compressed file instance source code in PHP

Source: Internet
Author: User
Tags explode zip

Do this before, no contact with the PHP compression this piece, online search some, most are PHP compression class, compression function, less than hundreds of lines, more than thousands of lines of code. This is very confusing to my novice, and I don't need to be so complicated. The last reference to the function manual, the understanding of a number of related functions, you will understand how to complete the.


PHP Zip File function


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


Source code Example:

The code is as follows Copy Code


<?php


Need to open configuration Php_zip.dll

Phpinfo ();

Header ("Content-type:text/html;charset=utf-8");


function Get_zip_originalsize ($filename, $path) {

First determine if the file you want to extract exists

if (!file_exists ($filename)) {

Die ("file $filename does not exist!") ");

}

$starttime = Explode (', microtime ()); Time to start decompression

The file name and path are converted to the Windows system default gb2312 encoding, otherwise you will not be able to read the

$filename = Iconv ("Utf-8", "gb2312", $filename);

$path = Iconv ("Utf-8", "gb2312", $path);

Open a compressed package

$resource = Zip_open ($filename);

$i = 1;

Traversal read a file inside a 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 ZIP package

$file _name = $path. Zip_entry_name ($dir _resource);

Split the Last "/", then intercept the path part with a string

$file _path = substr ($file _name,0,strrpos ($file _name, "/"));

If the path does not exist, a directory is created, and True indicates that you can create multilevel catalogs

if (!is_dir ($file _path)) {

mkdir ($file _path,0777,true);

}

Write file if it is not a directory

if (!is_dir ($file _name)) {

Read this file

$file _size = zip_entry_filesize ($dir _resource);

Maximum read 6M, if the file is too large, skip decompression, continue to the next

if ($file _size< (1024*1024*6)) {

$file _content = Zip_entry_read ($dir _resource, $file _size);

File_put_contents ($file _name, $file _content);

}else{www.111cn.net

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

}

}

Closes the current

Zip_entry_close ($dir _resource);

}

}

Turn off compression packs

Zip_close ($resource);

$endtime = Explode (', microtime ()); Time the decompression ended

$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);

$thistime = Round ($thistime, 3); Keep 3 as Decimal

echo "<p> decompression completed! , this decompression cost: $thistime seconds. </p> ";

}


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


?>


The test unzipped a small file of more than 300 KB and took 0.115 seconds to test the decompression of a more than 30 MB (web file, small file more) and took more than 20 seconds.


PHP Decompression Program


It's a little slower than the system, but it's good! Just getting started, the code is not good, but realize the pressure, and the code is more understandable than the online, if you see this article, I believe that you are helpful! The program uses the time that the program runs, the concrete can see:.


Finally, I think:

The code is as follows Copy Code

Maximum read 6M, if the file is too large, skip decompression, continue to the next

if ($file _size< (1024*1024*6)) {

$file _content = Zip_entry_read ($dir _resource, $file _size);

File_put_contents ($file _name, $file _content);

}

This piece does not very good, so the large file decompression will be no way, and so will be optimized.

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.