標籤:
php實現解壓zip檔案
1 function zip($filename, $path) 2 { 3 // $filename = ‘test.zip‘; 4 // $path = ‘./document/2016-05-11/test.zip‘; 5 $path_all = $_SERVER[‘DOCUMENT_ROOT‘] . sp_get_asset_upload_path(mb_substr($path, 2));//think_cmf的擷取檔案路勁 6 $file_name_head = mb_substr($path, 0, strrpos($path, "/")); 7 if (!file_exists($path_all)) { 8 echo ‘檔案不存在‘; 9 }10 //z轉碼 將檔案名稱和路徑轉成windows系統預設的gb2312編碼,否則將會讀取不到11 $filename = iconv(‘utf-8‘, ‘gb2312‘, $filename);12 $path_all = iconv(‘utf-8‘, ‘gb2312‘, $path_all);13 //開啟壓縮包14 $resource = zip_open($path_all);15 $i = 0;16 while ($dir_resource = zip_read($resource)) {17 if (zip_entry_open($resource, $dir_resource)) {18 $file_name = substr($path_all, 0, strrpos($path_all, "/")) . ‘/‘ . zip_entry_name($dir_resource);19 $dir_name = zip_entry_name($dir_resource);20 $file_path = substr($file_name, 0, strrpos($file_name, "/"));21 }22 if (!is_dir($file_path)) {23 mkdir($file_path, 0777, true);24 }25 if (!is_dir($file_name)) {26 $file_size = zip_entry_filesize($dir_resource);27 //最大讀取10M,如果檔案過大,跳過解壓,繼續下一個28 if ($file_size < (1024 * 1024 * 10)) {29 $file_content = zip_entry_read($dir_resource, $file_size);30 file_put_contents($file_name, $file_content);31 }32 //關閉當前33 zip_entry_close($dir_resource);34 }35 $dir_name = iconv(‘gb2312‘, ‘utf-8‘, $dir_name);36 $data[$i][filename] = $file_name_head . ‘/‘ . $dir_name;37 $data[$i][‘size‘] = $file_size;38 $i++;39 }40 zip_close($resource);41 return $data;42 }
php解壓zip檔案