PHP uses the Ziparchive function to achieve file compression and decompression,
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;//Create a new Ziparchive object if ($zip->open (' Test.zip ') ===true) { $zip->extractto (' Images ');//Assume decompression to the images folder under the current path
Compress the file into a zip file
$zip =new ziparchive; if ($zip->open (' Test.zip ', ziparchive::overwrite) ===true) { $zip->addfile (' image.txt ');// Assume that the file name is Image.txt, under the current path
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{
Iv. Package a folder into a zip file
function Addfiletozip ($path, $zip) { $handler =opendir ($path);//Open the current folder is 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 being read is a folder, the recursive addfiletozip ($path." /". $filename, $zip); } else{//Add files 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 method, Manipulate the root directory to be packaged and pass the Ziparchive object to the method
The above is the PHP implementation of the file compression and decompression of four different situations, there may be other circumstances did not complement the complete, in the subsequent article in succession, I hope this article on everyone's learning to help.
http://www.bkjia.com/PHPjc/1065577.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065577.html techarticle PHP uses the ziparchive function to achieve file compression and decompression, PHP ziparchive is a PHP extension class, you can easily achieve zip file compression and decompression, before using the first to ensure that PHP ...