12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- /**
- * function: 解压zip 格式的文件
- * author:friker
- * date:2015-15-14
- * reference:http://php.net/manual/zh/ref.zip.php
- * all rights reserved:wujiangwei123@126.com
- */
- namespace app\common\components;
- class Unzip{
- public function __construct(){
- //init code here...
- //header("content-type:text/html;charset=utf8");
- }
- function transcoding($fileName){
- $encoding = mb_detect_encoding($fileName,['UTF-8','GBK','BIG5','CP936']);
- if (DIRECTORY_SEPARATOR == '/'){ //linux
- $filename = iconv($encoding,'UTF-8',$fileName);
- }else{ //win
- $filename = iconv($encoding,'GBK',$fileName);
- }
- return $filename;
- }
- /**
- * 解压文件到指定目录
- *
- * @param string zip压缩文件的路径
- * @param string 解压文件的目的路径
- * @param boolean 是否以压缩文件的名字创建目标文件夹
- * @param boolean 是否重写已经存在的文件
- *
- * @return boolean 返回成功 或失败
- */
- public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){
- $fileList = array();
- $zip = new \ZipArchive;
- if ($zip->open($src_file) === TRUE) {
- if(!is_dir($dest_dir)) mkdir($dest_dir,0775,true);
- $docnum = $zip->numFiles;
- for($i = 0; $i < $docnum; $i++) {
- $statInfo = $zip->statIndex($i,\ZipArchive::FL_ENC_RAW);
- $filename = $this->transcoding($statInfo['name']);
- if($statInfo['crc'] == 0) {
- //新建目录
- if(!is_dir($dest_dir.'/'.substr($filename, 0,-1))) mkdir($dest_dir.'/'.substr($filename, 0,-1),0775,true);
- } else {
- //拷贝文件
- $fileList[] = $dest_dir.'/'.$filename;
- @copy('zip://'.$src_file.'#'.$zip->getNameIndex($i), $dest_dir.'/'.$filename);
- }
- }
- $zip->close();
- return $fileList;
- }else{
- return false;
- }
- }
- }
- ?>
|