用ZipArchive壓縮檔,這個是php的擴充類,自php5.2版本以後就已經支援這個擴充,如果你在使用的時候出現錯誤,查看下php.ini裡面的extension=php_zip.dll前面的分號有沒有去掉,然後再重啟Apache這樣才能使用這個類庫。
例1、產生zip 壓縮檔
複製代碼 代碼如下:
<?php
/* 產生zip 壓縮檔 */
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']);//去掉層級目錄
}
//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__ )); //網站路徑
$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);
例2 、壓縮檔夾下面的所有文
複製代碼 代碼如下:
<?php
/*
php zip壓縮檔夾下面的所有檔案
*/
class HZip
{
/**
* 添加檔案和子目錄的檔案到zip檔案
* @param string $folder
* @param ZipArchive $zipFile
* @param int $exclusiveLength Number of text to be 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)) {
// 添加子檔案夾
$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 be 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;//防止傳遞'folder' 檔案夾產生bug
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip檔案
$z->addEmptyDir($dirName);//建立檔案夾
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
//使用方法
HZip::zipDir('yourlife', 'yourlife.zip');
?>
1.ZipArchive::addEmptyDir
添加一個新的檔案目錄
2.ZipArchive::addFile
將檔案添加到指定zip壓縮包中。
3.ZipArchive::addFromString
添加的檔案同時將內容添加進去
4.ZipArchive::close
關閉ziparchive
5.ZipArchive::extractTo
將壓縮包解壓
6.ZipArchive::open
開啟一個zip壓縮包
7.ZipArchive::getStatusString
返回壓縮時的狀態內容,包括錯誤資訊,壓縮資訊等等
8.ZipArchive::deleteIndex
刪除壓縮包中的某一個檔案,如:deleteIndex(0)刪除第一個檔案
9.ZipArchive::deleteName
刪除壓縮包中的某一個檔案名稱,同時也將檔案刪除。