本文實例講述了PHP zip壓縮包操作類。分享給大家供大家參考,具體如下:
?php * Zip 文件包工具 * @author wengxianhu * @date 2013-08-05html' target='_blank'>class ZipFolder protected $zip; protected $root; protected $ignored_names; public function __construct(){ $this- zip = new ZipArchive; * 解壓zip文件到指定文件夾 * @access public * @param string $zipfile 壓縮文件路徑 * @param string $path 壓縮包解壓到的目標路徑 * @return booleam 解壓成功返回 true 否則返回 false public function unzip ($zipfile, $path) { if ($this- zip- open($zipfile) === true) { $file_tmp = @fopen($zipfile, rb $bin = fread($file_tmp, 15); //只讀15字節 各個不同文件類型,頭信息不一樣。 fclose($file_tmp); /* 只針對zip的壓縮包進行處理 */ if (true === $this- getTypeList($bin)) $result = $this- zip- extractTo($path); $this- zip- close(); return $result; else return false; return false; * 創建壓縮文件 * @access public * @param string $zipfile 將要生成的壓縮文件路徑 * @param strng $folder 將要被壓縮的文件夾路徑 * @param array $ignored 要忽略的文件列表 * @return booleam 壓縮包生成成功返回true 否則返回 false public function zip ($zipfile, $folder, $ignored = null) { $this- ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array(); if ($this- zip- open($zipfile, ZIPARCHIVE::CREATE) !== true) { throw new Exception( cannot open $zipfile /n $folder = substr($folder, -1) == / ? substr($folder, 0, strlen($folder)-1) : $folder; if(strstr($folder, / )) { $this- root = substr($folder, 0, strrpos($folder, / )+1); $folder = substr($folder, strrpos($folder, / )+1); $this- createZip($folder); return $this- zip- close(); * 遞歸添加文件到壓縮包 * @access private * @param string $folder 添加到壓縮包的文件夾路徑 * @param string $parent 添加到壓縮包的文件夾上級路徑 * @return void private function createZip ($folder, $parent=null) { $full_path = $this- root . $parent . $folder; $zip_path = $parent . $folder; $this- zip- addEmptyDir($zip_path); $dir = new DirectoryIterator($full_path); foreach($dir as $file) { if(!$file- isDot()) { $filename = $file- getFilename(); if(!in_array($filename, $this- ignored_names)) { if($file- isDir()) { $this- createZip($filename, $zip_path. / }else { $this- zip- addFile($full_path. / .$filename, $zip_path. / .$filename); * 讀取壓縮包文件與目錄列表 * @access public * @param string $zipfile 壓縮包文件 * @return array 文件與目錄列表 public function fileList($zipfile) { $file_dir_list = array(); $file_list = array(); if ($this- zip- open($zipfile) == true) { for ($i = 0; $i $this- zip- numFiles; $i++) { $numfiles = $this- zip- getNameIndex($i); if (preg_match( ///$/i , $numfiles)) $file_dir_list[] = $numfiles; else $file_list[] = $numfiles; return array( files = $file_list, dirs = $file_dir_list); * 得到文件頭與文件類型映射表 * @author wengxianhu * @date 2013-08-10 * @param $bin string 文件的二進制前一段字符 * @return boolean private function getTypeList ($bin) $array = array( array( 504B0304 , zip ) foreach ($array as $v) $blen = strlen(pack( H* , $v[0])); //得到文件頭標記字節數 $tbin = substr($bin, 0, intval($blen)); ///需要比較文件頭長度 if(strtolower($v[0]) == strtolower(array_shift(unpack( H* , $tbin)))) return true; return false;}
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
相關推薦:
PHP實現負載均衡下的session共用功能php技巧
PHPMAILER實現PHP發郵件功能php實例
以上就是關于PHP zip壓縮包操作類的介紹的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答