Php uses the ZipArchive function to compress and decompress files.

Source: Internet
Author: User
Tags decompress zip php ziparchive zip extension ziparchive
PHPZipArchive is a PHP Extension class that can easily compress and decompress ZIP files. before using PHPZIP, make sure that the extension is enabled, methods for enabling PHP amplification on different platforms are available online. if you have any questions, please contact us. Here are some common examples of using phpzipArchive to compress and decompress files. I. decompress the zip file $ zipnewZipArchive; create a new ZipArchive extension class for PHP ZipArchive, which can easily compress and decompress ZIP files, before using PHP, make sure that the php zip extension has been enabled. the specific method of enabling PHP extension is not mentioned here. you can use PHP extension methods on different platforms on the internet. if you have any questions, please contact us.

Here are some common examples of file compression and decompression using php zipArchive for your reference.

I. decompress the zip file

$ Zip = new ZipArchive; // Create a ZipArchive object

If ($ zip-> open('test.zip ') === TRUE ){
$ Zip-> extracloud ('images'); // assume that the file is decompressed to the images folder in the current path.
$ Zip-> close (); // close the processed zip file

}

2. compress the file into a zip file

$ Zip = new ZipArchive;

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

$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

Function addFileToZip ($ path, $ zip ){

$ Handler = opendir ($ path); // specify $ path to open the current folder.
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
}

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.