国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php ZipArchive壓縮函數詳解實例

2024-09-04 11:49:51
字體:
來源:轉載
供稿:網友

在php中生成zip文件我們只要使用一個php zip壓縮ZipArchive函數就可以了,下面小編來給大家總結兩個實現一個是利用ZipArchive生成zip,另一個壓縮文件夾下所有文件.

用ZipArchive壓縮文件,這個是php的擴展類,自php5.2版本以后就已經支持這個擴展,如果你在使用的時候出現錯誤,查看下php.ini里面的extension=php_zip.dll前面的分號有沒有去掉,然后再重啟Apache這樣才能使用這個類庫。

例1、生成zip 壓縮文件,代碼如下:

  1. /* 生成zip 壓縮文件 */ 
  2. function create_zip($files = array(),$destination = '',$overwrite = false) {  
  3.     //if the zip file already exists and overwrite is false, return false  
  4.     if(file_exists($destination) && !$overwrite) { return false; }  
  5.     //vars  
  6.     $valid_files = array();  
  7.     //if files were passed in...  
  8.     if(is_array($files)) {  
  9.         //cycle through each file  
  10.         foreach($files as $file) {  
  11.             //make sure the file exists  
  12.             if(file_exists($file)) {  
  13.                 $valid_files[] = $file;  
  14.             }  
  15.         }  
  16.     }  
  17.     //if we have good files...  
  18.     if(count($valid_files)) {  
  19.         //create the archive  
  20.         $zip = new ZipArchive();  
  21.         if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {  
  22.             return false;  
  23.         }  
  24.         //add the files  
  25.         foreach($valid_files as $file) {  
  26.             $file_info_arrpathinfo($file);  
  27.             $zip->addFile($file,$file_info_arr['basename']);//去掉層級目錄  
  28.         }  
  29.         //debug  
  30.         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;  
  31.  
  32.         //close the zip -- done!  
  33.         $zip->close();  
  34.  
  35.         //check to make sure the file exists  
  36.         return file_exists($destination);  
  37.     }  
  38.     else 
  39.     {  
  40.         return false;  
  41.     }  
  42. }  
  43.  
  44. define('ROOTPATH',dirname ( __FILE__ )); //網站路徑  
  45.  
  46. $files_to_zip = array(  
  47.     ROOTPATH.DIRECTORY_SEPARATOR.'PHP+jQuery+Cookbook.pdf',  
  48.     ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv' 
  49. );  
  50. //if true, good; if false, zip creation failed  
  51. $filename='my-archive.zip';  
  52. $result = create_zip($files_to_zip,$filename); 

例2 、壓縮文件夾下面的所有文件,代碼如下:

  1. <?php  
  2. /*  
  3. php zip壓縮文件夾下面的所有文件  
  4. */ 
  5. class HZip  
  6. {  
  7.   /**  
  8.    * 添加文件和子目錄的文件到zip文件  
  9.    * @param string $folder  
  10.    * @param ZipArchive $zipFile  
  11.    * @param int $exclusiveLength Number of text to be exclusived from the file path.  
  12.    */ 
  13.   private static function folderToZip($folder, &$zipFile$exclusiveLength) {  
  14.     $handle = opendir($folder);  
  15.     while (false !== $f = readdir($handle)) {  
  16.       if ($f != '.' && $f != '..') {  
  17.         $filePath = "$folder/$f";  
  18.         // Remove prefix from file path before add to zip.  
  19.         $localPath = substr($filePath$exclusiveLength);  
  20.         if (is_file($filePath)) {  
  21.           $zipFile->addFile($filePath$localPath);  
  22.         } elseif (is_dir($filePath)) {  
  23.           // 添加子文件夾  
  24.           $zipFile->addEmptyDir($localPath);  
  25.           self::folderToZip($filePath$zipFile$exclusiveLength);  
  26.         }  
  27.       }  
  28.     }  
  29.     closedir($handle);  
  30.   }  
  31.  
  32.   /**  
  33.    * Zip a folder (include itself).  
  34.    * Usage:  
  35.    *   HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');  
  36.    *  
  37.    * @param string $sourcePath Path of directory to be zip.  
  38.    * @param string $outZipPath Path of output zip file.  
  39.    */ 
  40.   public static function zipDir($sourcePath$outZipPath)  
  41.   {  
  42.     $pathInfo = pathInfo($sourcePath);  
  43.     $parentPath = $pathInfo['dirname'];  
  44.     $dirName = $pathInfo['basename'];  
  45.     $sourcePath=$parentPath.'/'.$dirName;//防止傳遞'folder' 文件夾產生bug  
  46.     $z = new ZipArchive();  
  47.     $z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件  
  48.     $z->addEmptyDir($dirName);//建立文件夾  
  49.     self::folderToZip($sourcePath$zstrlen("$parentPath/"));  
  50.     $z->close();  
  51.   }  
  52. }  
  53.  
  54. //使用方法  
  55. HZip::zipDir('yourlife''yourlife.zip');  
  56. ?> 

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

刪除壓縮包中的某一個文件名稱,同時也將文件刪除。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偃师市| 清河县| 天气| 景德镇市| 凤冈县| 赣榆县| 浮梁县| 湄潭县| 江阴市| 乌兰浩特市| 上饶市| 政和县| 雅江县| 辽阳市| 乌恰县| 巴林左旗| 镇康县| 岳阳市| 盐津县| 甘南县| 临泉县| 涪陵区| 濮阳县| 山丹县| 临漳县| 轮台县| 毕节市| 肥西县| 友谊县| 鲁甸县| 淮安市| 镇安县| 永修县| 都安| 文成县| 南陵县| 四平市| 舟曲县| 仲巴县| 荥经县| 九寨沟县|