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
}