With ziparchive compressed files, this is the PHP extension class, since the php5.2 version has been supported this extension, if you are using the error, check the php.ini inside the EXTENSION=PHP_ Zip.dll in front of the semicolon has not been removed, and then restart Apache to use this class library.
Example 1, generating a zip compressed file
Copy CodeThe code is as follows:
/* 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__)); 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, compressed folder under all the text
Copy CodeThe code is as follows:
/*
All files under the PHP Zip archive folder
*/
Class Hzip
{
/**
* Add files and subdirectories of files 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 the file path before add to zip.
$localPath = substr ($filePath, $exclusiveLength);
if (Is_file ($filePath)) {
$zipFile->addfile ($filePath, $localPath);
} elseif (Is_dir ($filePath)) {
Add sub-folders
$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 a bug from passing ' folder ' folders
$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 archive.
3.ziparchive::addfromstring
Add the file and add the content in
4.ziparchive::close
Close Ziparchive
5.ziparchive::extractto
Unzip the compressed package
6.ziparchive::open
Open a ZIP archive package
7.ziparchive::getstatusstring
Returns the state content of compression, including error messages, compressed information, and so on
8.ZipArchive::d Eleteindex
Delete a file in a compressed package, such as: Deleteindex (0) Delete the first file
9.ZipArchive::d eletename
Deletes a file name from a compressed package, and also deletes the file.
http://www.bkjia.com/PHPjc/825095.html www.bkjia.com true http://www.bkjia.com/PHPjc/825095.html techarticle with ziparchive compressed files, this is the PHP extension class, since the php5.2 version has been supported this extension, if you are using the error, check the php.ini inside the extensi ...