PHP ziparchive is a PHP extension class, you can easily achieve the zip file compression and decompression, before using the first to ensure that the PHP zip extension has been opened, the specific opening method does not say, different platforms to open the method of PHP amplification online have, if there is doubt welcome communication. Here's a look at the usual examples for reference.
First, unzip the zip file
$zip = new ziparchive;//Creates a Ziparchive object */* The zip file is processed by Ziparchive object $zip->open This method parameter represents the ZIP file name processed. If the operation on the zip file object succeeds, $zip->open This method will return true*/if ($zip->open (' test.zip ') = = = TRUE) {$zip->extractto (' images ');// Assume that you unzipped the images folder under the current path to a subfolder php$zip->close ();//Close the processed zip file}
Compress the file into a zip file
$zip = new ziparchive;/* $zip->open The first parameter of this method represents the zip file name processed. The second parameter represents the processing mode, ziparchive::overwrite means that if the zip file exists, it overwrites the original zip file. If the parameter uses Ziparchive::create, the system will add content to the original zip file. If you are not adding content to a zip file more than once, we recommend using Ziparchive::overwrite. Using these two parameters, if the zip file does not exist, the system will be automatically created. If the operation on the zip file object succeeds, $zip->open This method returns true*/if ($zip->open (' Test.zip ', ziparchive::overwrite) = = = TRUE) {$zip- AddFile (' image.txt ');//Assume that the file name to be 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. /* loop all files and folders under the Read folder where $filename = Readdir ($handler) is the read file name is assigned to $filename each time the loop is looped, so $filename!== false is also allowed in order not to be trapped in a dead loop. Be sure to use!==, because if a file name is called ' 0 ', or if some are considered false by the system, you will stop looping */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 being read is a folder, the recursive 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) ; 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}
The Ziparchive method is as follows:
Ziparchive::addemptydir-add a new directory
Ziparchive::addfile-adds a file to a ZIP archive from the given path
Ziparchive::addfromstring-add a file to a ZIP archive using its contents
Ziparchive::close-close the Active archive (opened or newly created)
Ziparchive::d eleteindex-delete An entry in the archive using its index
Ziparchive::d eletename-delete An entry in the archive using its name
Ziparchive::extractto-extract the archive contents
Ziparchive::getarchivecomment-returns the Zip archive comment
Ziparchive::getcommentindex-returns the comment of an entry using the entry index
Ziparchive::getcommentname-returns the comment of an entry using the entry name
Ziparchive::getfromindex-returns the entry contents using its index
Ziparchive::getfromname-returns the entry contents using its name
Ziparchive::getnameindex-returns the name of an entry using its index
Ziparchive::getstatusstring-returns the status error message, System and/or ZIP messages
Ziparchive::getstream-get a file handler to the entry defined by its name (read only).
Ziparchive::locatename-returns the index of the entry in the archive
Ziparchive::open-open a ZIP file archive
Ziparchive::renameindex-renames an entry defined by its index
Ziparchive::renamename-renames an entry defined by its name
Ziparchive::setarchivecomment-set the comment of a ZIP archive
Ziparchive::setcommentindex-set the comment of an entry defined by its index
Ziparchive::setcommentname-set the comment of an entry defined by its name
Ziparchive::statindex-get the details of an entry defined by its index.
Ziparchive::statname-get the details of an entry defined by its name.
Ziparchive::unchangeall-undo all changes do in the archive
Ziparchive::unchangearchive-revert All global changes do in the archive.
Ziparchive::unchangeindex-revert all changes do to a entry at the given index
Ziparchive::unchangename-revert all changes do to a entry with the given name.
The above describes the PHP ziparchive implementation compression decompression zip file, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.