Php ZipArchive class usage instance, phpziparchive instance

Source: Internet
Author: User
Tags decompress zip php ziparchive ziparchive

Php ZipArchive class usage instance, phpziparchive instance

This article describes the php ZipArchive class usage and shares it with you for your reference. The details are as follows:

Generally, php5.2 supports the ZipArchive class, And php4 can only use the zip function. In fact, before the official implementation of the zip class, Daniel has contributed to the packaging and decompression of the zip file. Now php contains the ZipArchive class, which is preferred. This class can be used to create and decompress zip files and directly read the content in the zip package, which is very convenient. Here we mainly summarize the process of reading and decompressing.

Decompress a package to the specified directory:

Copy codeThe Code is as follows: <? Php
$ Zip = new ZipArchive;
If ($ zip-> open('test.zip ') === TRUE ){
$ Zip-> extracelist ('/my/destination/dir /');
$ Zip-> close ();
Echo 'OK ';
} Else {
Echo 'failed ';
}
?>
If you only need to read the content of a file in the package, you need the file name or the index value of the file.

Copy codeThe Code is as follows: <? Php
$ Zip = new ZipArchive;
If ($ zip-> open('test.zip ') === TRUE ){
Echo $ zip-> getFromName ('example. php ');
$ Zip-> close ();
}
?>
If example. php is in a directory, you must add a path to obtain the content.

If you only know the file name, but do not know the specific path of the file, you can search for the index of the specified file name, and then retrieve the content by the index.

Copy codeThe Code is as follows: <? Php
$ Zip = new ZipArchive;
If ($ zip-> open('test.zip ') === TRUE ){
$ Index = $ zip-> locateName ('example. php', ZIPARCHIVE: FL_NOCASE | ZIPARCHIVE: FL_NODIR );
$ Contents = $ zip-> getFromIndex ($ index );
}
?>
The locateName method is used to obtain the index. If a file with the same name exists in multiple paths in the compressed package, it seems that only the first index can be returned. To obtain the index of all files with the same name, you can only use the stupid method, loop search.

Copy codeThe Code is as follows: <? Php
$ Zip = new ZipArchive;
If ($ zip-> open('test.zip ') === TRUE ){
For ($ I = 0; $ I <$ zip-> numFiles; $ I ++)
{
If (substr_count ($ zip-> getNameIndex ($ I), 'example. php')> 0 ){
$ Contents = $ zip-> getFromIndex ($ I );
}
}
}
?>

I hope this article will help you with php programming.


Php ZipArchive can generate a compressed package in a specified directory

Of course.

The following code generates the aaaa.zip file under the/tmpdirectory:
$ Zip = new ZipArchive (); $ zip-> open ('/tmp/aaaa.zip', ZipArchive: CREATE); $ zip-> addEmptyDir ('dir1 '); $ zip-> close ();

For PHP zipArchive ()

It may be caused by restrictions on the available memory of PHP.
Memory_limit
Set the value of this option to a larger value.

You can also set it in the php file, for example:

Ini_set ('memory _ limit ', '32m ');

Supplement:
If 32 M is not enough, add it up.
Try setting with the php. ini file.
 

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.