PhpZipArchive class usage example _ PHP Tutorial

Source: Internet
Author: User
Tags php ziparchive ziparchive
Details about phpZipArchive class instances. PhpZipArchive is a built-in function of php. it can compress and decompress files. However, before using this function, we must. before using extensionphp_zip.dll in ini, php ZipArchive is a built-in function of php. it can compress and decompress files, but before using this function, we must use php. in ini, remove the semicolon before extension = php_zip.dll, and restart Apache to use this class library.

Optional ziparchive parameters

1. ZipArchive: addEmptyDir

Add a new file directory

2. ZipArchive: addFile

Add the file to the specified zip package.

3. ZipArchive: addFromString

Add the content to the added file at the same time.

4. ZipArchive: close

Disable ziparchive

5. ZipArchive: extrac.pdf

Decompress the package

6. ZipArchive: open

Open a zip package

7. ZipArchive: getStatusString

Returns the status content during compression, including error information and compression information.

8. ZipArchive: deleteIndex

Delete a file in the compressed package, for example, deleteIndex (0). delete the first file.

9. ZipArchive: deleteName

Delete a file name in the package and delete the file.
......
*/


Instance

I. decompress the zip file

The code is as follows:
$ Zip = new ZipArchive; // Create a ZipArchive object
/*
Process zip files with ZipArchive objects
$ Zip-> open parameter indicates the zip file name to be processed.
If the operation on the zip file object is successful, $ zip-> open returns TRUE.
*/
If ($ zip-> open('test.zip ') = TRUE)
{
$ Zip-> extracloud ('images'); // suppose you want to decompress the package to the php
$ Zip-> close (); // close the processed zip file
}

2. compress the file into a zip file

The code is as follows:
$ Zip = new ZipArchive;
/*
$ Zip-> open the first parameter of this method indicates the zip file name to be processed.
The second parameter indicates the processing mode. ZipArchive: OVERWRITE indicates that if the zip file exists, the original zip file will be overwritten.
If the parameter uses ZIPARCHIVE: CREATE, the system will add content to the original zip file.
If you do not want to add content to the zip file multiple times, we recommend that you use ZipArchive: OVERWRITE.
If the zip file does not exist, the system automatically creates a new one.
If the operation on the zip file object is successful, $ zip-> open returns TRUE.
*/
If ($ zip-> open('test.zip ', ZipArchive: OVERWRITE) === TRUE)
{
$ Zip-> addFile('image.txt '); // the file name is image.txt, in the current path
$ Zip-> close ();
}

3. add the content appended to the zip file

The code is as follows:
$ Zip = new ZipArchive;
$ Res = $ zip-> open('test.zip ', ZipArchive: CREATE );
If ($ res = TRUE ){
$ Zip-> addFromString('test.txt ', 'File content goes here ');
$ Zip-> close ();
Echo 'OK ';
} Else {
Echo 'failed ';
}

4. package folders into zip files

The code is as follows:

Function addFileToZip ($ path, $ zip ){
$ Handler = opendir ($ path); // specify $ path to open the current folder.
/*
Read all files and folders in the folder cyclically
$ Filename = readdir ($ handler) assigns the read file name to $ filename during each loop,
To avoid getting stuck in an endless loop, you need to make $ filename! = False.
Be sure to use it! =, Because if the name of a file is '0' or is regarded as false by the system, use! = Will stop the loop
*/
While ($ filename = readdir ($ handler ))! = False ){
If ($ filename! = "." & $ Filename! = "..") {// The folder names are '.' and '..'. do not operate on them.
If (is_dir ($ path. "/". $ filename) {// recursion
AddFileToZip ($ path. "/". $ filename, $ zip );
} Else {// add the file to the zip object
$ Zip-> addFile ($ path. "/". $ filename );
}
}
}
@ Closedir ($ path );
}

$ Zip = new ZipArchive ();
If ($ zip-> open('images.zip ', ZipArchive: OVERWRITE) === TRUE ){
AddFileToZip ('images/', $ zip); // call a method to operate the root directory to be packaged, and pass the ZipArchive object to the method
$ Zip-> close (); // close the processed zip file
}


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.

The code is as follows:


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

The code is as follows:

$ 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 );
}
}
}
?>

Http://www.bkjia.com/PHPjc/632774.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632774.htmlTechArticlephp ZipArchive can be said to be a function of php, which can be compressed and decompressed on the file, but before using this class, we must put extension = php_zip.dll in php. ini...

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.