PHP file compression

Source: Internet
Author: User
Tags ziparchive

1.php File Compression Code:

$zip = new Ziparchive;
if ($zip->open (' Aaa.zip ', ziparchive::overwrite) ===true) {//aaa.zip to compress into a ZIP file name
$zip->addfile (' c:/wamp/www/test_demo/phpunzip/aaa.txt '); A scarf that needs compression.
$zip->close ();
echo ' OK ';
} else {
Echo ' failed ';
}

2. Unzip

$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
}


3. append files to compressed files

$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. folder compressed into 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
}

PHP file compression

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.