With ziparchive compressed file, this is the extension of PHP class, since the php5.2 version has been supported by this extension, if you are in use when there are errors, look at the php.ini inside the EXTENSION=PHP_ Zip.dll before the semicolon is removed, and then restart Apache so you can use this class library.
Example 1, generating zip compressed files
Copy Code code as follows:
<?php
/* Generate Zip Compressed file */
function Create_zip ($files = Array (), $destination = ", $overwrite = False) {
If the zip file already exists and overwrite is false, return false
if (file_exists ($destination) &&! $overwrite) {return false;}
VARs
$valid _files = Array ();
If files were passed in ...
if (Is_array ($files)) {
Cycle through each file
foreach ($files as $file) {
Make sure the file exists
if (file_exists ($file)) {
$valid _files[] = $file;
}
}
}
If we have good files ...
if (count ($valid _files)) {
Create the Archive
$zip = new Ziparchive ();
if ($zip->open ($destination, $overwrite? Ziparchive::overwrite:ziparchive::create)!== true) {
return false;
}
Add the Files
foreach ($valid _files as $file) {
$file _info_arr= pathinfo ($file);
$zip->addfile ($file, $file _info_arr[' basename '));//Remove Hierarchy Directory
}
Debug
Echo ' The zip archive contains ', $zip->numfiles, ' files with a status of ', $zip->status;
Close the zip--done!
$zip->close ();
Check to make sure the file exists
return file_exists ($destination);
}
Else
{
return false;
}
}
Define (' RootPath ', DirName (__file__)); Web site Path
$files _to_zip = Array (
RootPath. Directory_separator. ' Php+jquery+cookbook.pdf ',
RootPath. Directory_separator. ' Turbolisterzerotemplate.csv '
);
If true, good; If False, Zip creation failed
$filename = ' My-archive.zip ';
$result = Create_zip ($files _to_zip, $filename);
Example 2, compress all the text below the folder
Copy Code code as follows:
<?php
/*
All files under the php Zip zipped folder
*/
Class Hzip
{
/**
* Add files and subdirectories to the zip file
* @param string $folder
* @param ziparchive $zipFile
* @param int $exclusiveLength Number of text to is exclusived from the file path.
*/
private static function Foldertozip ($folder, & $zipFile, $exclusiveLength) {
$handle = Opendir ($folder);
while (false!== $f = Readdir ($handle)) {
if ($f!= '. ' && $f!= ' ... ') {
$filePath = "$folder/$f";
Remove prefix from file path before add to zip.
$localPath = substr ($filePath, $exclusiveLength);
if (Is_file ($filePath)) {
$zipFile->addfile ($filePath, $localPath);
} elseif (Is_dir ($filePath)) {
Add Sub Folder
$zipFile->addemptydir ($localPath);
Self::foldertozip ($filePath, $zipFile, $exclusiveLength);
}
}
}
Closedir ($handle);
}
/**
* Zip a folder (include itself).
* Usage:
* Hzip::zipdir ('/path/to/sourcedir ', '/path/to/out.zip ');
*
* @param string $sourcePath Path of directory to is zip.
* @param string $outZipPath Path of output zip file.
*/
public static function Zipdir ($sourcePath, $outZipPath)
{
$pathInfo = PathInfo ($sourcePath);
$parentPath = $pathInfo [' dirname '];
$dirName = $pathInfo [' basename '];
$sourcePath = $parentPath. ' /'. $dirName;//Prevent passing ' folder ' folders create bugs
$z = new Ziparchive ();
$z->open ($outZipPath, ziparchive::create);//Build ZIP file
$z->addemptydir ($dirName);//Create Folder
Self::foldertozip ($sourcePath, $z, strlen ("$parentPath/"));
$z->close ();
}
}
How to use
Hzip::zipdir (' Yourlife ', ' yourlife.zip ');
?>
1.ziparchive::addemptydir
Add a new file directory
2.ziparchive::addfile
Adds a file to the specified zip compression package.
3.ziparchive::addfromstring
Add a file to add content at the same time
4.ziparchive::close
Close Ziparchive
5.ziparchive::extractto
Unzip the compressed package
6.ziparchive::open
Open a ZIP compression package
7.ziparchive::getstatusstring
Returns the state content at the time of compression, including error messages, compression information, and so on
8.ZipArchive::d Eleteindex
Deletes a file in a compressed package, such as: Deleteindex (0) Deleting the first file
9.ZipArchive::d eletename
Deletes a file name from a compressed package and deletes the file as well.