Compress a file
We build a file into a compressed package.
<?php
$path = "C:/wamp/www/log.txt";
$filename = "Test.zip";
$zip = new Ziparchive ();
$zip->open ($filename, ziparchive::create); Open the compression pack
$zip->addfile ($path, basename ($path)); Add a file to the compressed package
$zip->close (); Turn off compression packs
The code above generates the C:/wamp/www/log.txt file compression Test.zip and saves it in the current directory. Compress multiple Files
Compressing multiple files, in fact, is addfile execution many times, can be achieved through the traversal of the array.
<?php
$fileList = Array (
"C:/wamp/www/log.txt",
"c:/wamp/www/weixin.class.php"
);
$filename = "Test.zip";
$zip = new Ziparchive ();
$zip->open ($filename, ziparchive::create); Open the compression bundle
foreach ($fileList as $file) {
$zip->addfile ($file, basename ($file)); Add files to the compressed package
$zip->close (); Turn off compression packs
Compress a directory
<?php
function Addfiletozip ($path, $zip) {
$handler =opendir ($path);//Open current folder specified by $path. While
(($filename =readdir ($handler))!==false) {
if ($filename!= "." && $filename!= "...") The {//folder file name is '. ' and '.. ', do not manipulate them
if (Is_dir ($path.) /". $filename)) {//If one of the objects read is a folder, 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 (' Rsa.zip ', ziparchive::overwrite) = = TRUE) {
addfiletozip (' rsa/', $zip);//calling methods, manipulating the root directory to be packaged, and passes the Ziparchive object to the method
$zip->close ();//Close the processed zip file
}
Compress and download zip packages
My time, we need to pack, provide downloads, and then delete the zip package.
Can be divided into the following steps: To determine the given path, is a folder, or a file. Folders also need to traverse the add file. Set the related file headers and use the ReadFile function to provide downloads. To delete a compressed package using the Unlink function
<?php function Addfiletozip ($path, $zip) {$handler =opendir ($path);//Open current folder specified by $path. while (($filename =readdir ($handler))!==false) {if ($filename!= "." && $filename!= "...") The {//folder file name is '. ' and '.. ', do not manipulate them if (Is_dir ($path.) /". $filename)) {//If one of the objects read is a folder, 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 (' Rsa.zip ', ziparchive::overwrite) = = TRUE) {$path = ' rsa/';
if (Is_dir ($path)) {//Give a folder, Package folder Addfiletozip ($path, $zip);
}else if (Is_array ($path)) {//is given the file path foreach ($path as $file) {$zip->addfile ($file) in the form of an array;
}else{//Only give a file $zip->addfile ($path); } $zip->close (); Closes the processed zip file}