php解压zip文件
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 }