標籤:檔案壓縮 php
PclZip檔案壓縮實現(推薦)
我在做項目是的時候有個打包下載的需求:把上傳的多個檔案壓縮成一個檔案並下載,我用的比較強大的PclZip類實現的。我的用的是thinkphp3.2架構開發的,具體實現代碼如下
/** * 壓縮檔 * @param $zipName 壓縮的檔案名稱 * @param $fileName 要壓縮的檔案路徑的數組或字串 * @param $savePath 要儲存的路徑 * @return bool */public function createZip($zipName,$fileName,$savePath){ //載入類 import("Org.Io.PclZip"); $archive = new \PclZip($zipName); $list = $archive->create($fileName,PCLZIP_OPT_REMOVE_ALL_PATH); //壓縮檔 if($list == 0){ return false; } //建立目錄,不存在就建立 if( !is_dir( $savePath)) { mkdir( $savePath); } //檢查檔案是否存在 if(!file_exists($zipName)){ return false; } //移動檔案 create方法不能指定壓縮檔儲存路徑,預設是儲存在目前的目錄。 rename ($zipName,$savePath.$zipName); return $savePath.$zipName; }
其他參考資料:http://blog.csdn.net/gumanren/article/details/5520173
其他實現方式
1.PHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南 :
http://www.jb51.net/article/61678.htm
http://www.cnblogs.com/keleyu/p/4273120.html
2.PHP壓縮與解壓Zip(PHPZip類)
http://www.oschina.net/code/snippet_167936_6898
php 檔案壓縮