PHP ziparchive is a PHP extension class, you can easily achieve zip file compression and decompression, before using the first to ensure that the PHP zip extension has been opened, the specific opening method here does not say, different platforms to open the method of PHP amplification online have, if there is doubt welcome communication.
Here's a general example of compressing and decompressing files using PHP ziparchive for reference.
First, unzip the zip file
$zip =new ziparchive;//to create a new Ziparchive object
if ($zip->open (' Test.zip ') ===true) {
$zip->extractto (' images ');//Assume decompression to the images folder under the current path
$zip->close ();//Close the processed zip file
}
Compress the file into a zip file
$zip =new ziparchive;
if ($zip->open (' Test.zip ', ziparchive::overwrite) ===true) {
$zip->addfile (' image.txt ');//Assuming that the name of the file being added is Image.txt, under the current path
$zip->close ();
}
Third, file append content to 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 ';
}
Iv. Package a folder into a zip file
function Addfiletozip ($path, $zip) {
$handler =opendir ($path); Opens the current folder specified by $path.
while (($filename =readdir ($handler))!==false) {
if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them
if (Is_dir ($path. " /". $filename)) {//If an object read is a folder, the recursive
Addfiletozip ($path. " /". $filename, $zip);
}else{//Adding files to a Zip object
$zip->addfile ($path. " /". $filename);
}
}
}
@closedir ($path);
}
$zip =new ziparchive ();
if ($zip->open (' Images.zip ', ziparchive::overwrite) = = = TRUE) {
Addfiletozip (' images/', $zip); Invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method
$zip->close (); Close the processed zip file
}