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

首頁 > 開發(fā) > PHP > 正文

php生成zip壓縮文件兩個實例詳解

2024-05-04 21:48:26
字體:
來源:轉載
供稿:網友

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

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

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

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

例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. ?> 

ziparchive 可選參數(shù):

1.ZipArchive::addEmptyDir

添加一個新的文件目錄

2.ZipArchive::addFile

將文件添加到指定zip壓縮包中。

3.ZipArchive::addFromString

添加的文件同時將內容添加進去

4.ZipArchive::close

關閉ziparchive

5.ZipArchive::extractTo

將壓縮包解壓

6.ZipArchive::open

打開一個zip壓縮包

7.ZipArchive::getStatusString

返回壓縮時的狀態(tài)內容,包括錯誤信息,壓縮信息等等

8.ZipArchive::deleteIndex

刪除壓縮包中的某一個文件,如:deleteIndex(0)刪除第一個文件

9.ZipArchive::deleteName

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵山县| 邵东县| 深水埗区| 邵阳县| 拜城县| 深州市| 安新县| 贵州省| 闸北区| 山阳县| 鄄城县| 洱源县| 永济市| 乐昌市| 林芝县| 湟中县| 综艺| 孟村| 沈阳市| 原平市| 潞西市| 武陟县| 峨眉山市| 兴隆县| 嘉峪关市| 玛曲县| 望都县| 开远市| 桃江县| 五台县| 宁化县| 岐山县| 藁城市| 冷水江市| 永济市| 静安区| 伽师县| 囊谦县| 英德市| 汝州市| 牡丹江市|