用ZipArchive压缩文件,这个是php的扩展类,自php5.2版本以后就已经支持这个扩展,如果你在使用的时候出现错误,查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉,然后再重启Apache这样才能使用这个类库。
例1、生成zip 压缩文件
<?php <BR>/* 生成zip 压缩文件 */<BR>function create_zip($files = array(),$destination = '',$overwrite = false) { <BR> //if the zip file already exists and overwrite is false, return false <BR> if(file_exists($destination) && !$overwrite) { return false; } <BR> //vars <BR> $valid_files = array(); <BR> //if files were passed in... <BR> if(is_array($files)) { <BR> //cycle through each file <BR> foreach($files as $file) { <BR> //make sure the file exists <BR> if(file_exists($file)) { <BR> $valid_files[] = $file; <BR> } <BR> } <BR> } <BR> //if we have good files... <BR> if(count($valid_files)) { <BR> //create the archive <BR> $zip = new ZipArchive(); <BR> if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { <BR> return false; <BR> } <BR> //add the files <BR> foreach($valid_files as $file) { <BR> $file_info_arr= pathinfo($file); <BR> $zip->addFile($file,$file_info_arr['basename']);//去掉层级目录 <BR> } <BR> //debug <BR> //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; <br><br> //close the zip -- done! <BR> $zip->close(); <br><br> //check to make sure the file exists <BR> return file_exists($destination); <BR> } <BR> else<BR> { <BR> return false; <BR> } <BR>} <br><br>define('ROOTPATH',dirname ( __FILE__ )); //网站路径 <br><br>$files_to_zip = array( <BR> ROOTPATH.DIRECTORY_SE<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div><pre>搞gaodaima代码
PARATOR.’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 <BR>/* <BR>php zip压缩文件夹下面的所有文件 <BR>*/<BR>class HZip <BR>{ <BR> /** <BR> * 添加文件和子目录的文件到zip文件 <BR> * @param string $folder <BR> * @param ZipArchive $zipFile <BR> * @param int $exclusiveLength Number of text to be exclusived from the file path. <BR> */<BR> private static function folderToZip($folder, &$zipFile, $exclusiveLength) { <BR> $handle = opendir($folder); <BR> while (false !== $f = readdir($handle)) { <BR> if ($f != '.' && $f != '..') { <BR> $filePath = "$folder/$f"; <BR> // Remove prefix from file path before add to zip. <BR> $localPath = substr($filePath, $exclusiveLength); <BR> if (is_file($filePath)) { <BR> $zipFile->addFile($filePath, $localPath); <BR> } elseif (is_dir($filePath)) { <BR> // 添加子文件夹 <BR> $zipFile->addEmptyDir($localPath); <BR> self::folderToZip($filePath, $zipFile, $exclusiveLength); <BR> } <BR> } <BR> } <BR> closedir($handle); <BR> } <br><br> /** <BR> * Zip a folder (include itself). <BR> * Usage: <BR> * HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); <BR> * <BR> * @param string $sourcePath Path of directory to be zip. <BR> * @param string $outZipPath Path of output zip file. <BR> */<BR> public static function zipDir($sourcePath, $outZipPath) <BR> { <BR> $pathInfo = pathInfo($sourcePath); <BR> $parentPath = $pathInfo['dirname']; <BR> $dirName = $pathInfo['basename']; <BR> $sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug <BR> $z = new ZipArchive(); <BR> $z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件 <BR> $z->addEmptyDir($dirName);//建立文件夹 <BR> self::folderToZip($sourcePath, $z, strlen("$parentPath/")); <BR> $z->close(); <BR> } <BR>} <br><br>//使用方法 <BR>HZip::zipDir('yourlife', 'yourlife.zip'); <BR>?><BR>
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
删除压缩包中的某一个文件名称,同时也将文件删除。