PHP 解压文件 ZipArchive 中文文件名丢失/乱码问题

PHP 解压文件 ZipArchive 中文文件名丢失/乱码问题     PHP 解压中文或解压文件中 文件名存在中文  

$file = '/log/你好.zip';  //文件路径 如/log/test/123.zip
$outputPath = '/opt';   //解压后存储的路径
$zip = new \ZipArchive();
$openRes = $zip->open($file);
if ($openRes === 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) {
            //新建目录
            mkdir($outputPath.'/'.substr($filename, 0,-1));
        } else {
            //拷贝文件
            copy('zip://'.$file.'#'.$zip->getNameIndex($i), $outputPath.'/'.$filename);
        }
    }
    // $zip->extractTo($outputPath);
    $zip->close();
    return ['status'=>true,'path'=>$outputPath];
}

/**
  *压缩文件中文转码 
  *@param fileName 文件名
  */
public 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;
}

以下是解压文件 返回解压文件列表

    private $ErrMsg = '';

   /**
    * zip解压方法
    * @param string $filePath 压缩包所在地址 【绝对文件地址】d:/test/123.zip
    * @param string $path 解压路径 【绝对文件目录路径】d:/test
    * @return r_name   解压文件列表
    */
    public function unzip($filePath, $path) {
        $r_name = [];
        $zip = new \ZipArchive;//新建一个ZipArchive的对象
        /*
        通过ZipArchive的对象处理zip文件
        $zip->open这个方法的参数表示处理的zip文件名。
        如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
        */
        $res = $zip->open(mb_convert_encoding($filePath, "utf-8", "auto"));
        if ($res === TRUE){
            if (!is_dir(mb_convert_encoding($path, "utf-8", "auto"))) {
                mkdir(mb_convert_encoding($path, "utf-8", "auto"), 0777, true);
            }    
            //$zip->extractTo($toDir);
            $docnum = $zip->numFiles;
            for($i = 0; $i < $docnum; $i++) {
                //$statInfo = $zip->statIndex($i);
                $statInfo = $zip->statIndex($i,\ZipArchive::FL_ENC_RAW);
                $filename = $this->transcoding($statInfo['name']);  //转码
                $r_name[] = $filename;
                if($statInfo['crc'] == 0) { 
                    //新建目录
                    mkdir(mb_convert_encoding($path, "utf-8", "auto"), 0777, true);
                } else {
                    //拷贝文件,特别的改动,iconv的位置决定copy能不能work
                    if(copy('zip://'.$filePath.'#'.$zip->getNameIndex($i),  $path.'/'.$filename) == false){
                        $this->ErrMsg = 'faild to copy';
                        return false;
                    }
                }
            }
            $zip->close();//关闭处理的zip文件
            return $r_name;
        }else{
            //$this->ErrMsg = 'failed, code:'.$res;
            $this->ErrMsg = '解压文件异常';
            return false;
        }
    }

    /**
     *压缩文件中文转码 
     *@param fileName 文件名
     */
    public 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;
    }

 

posted @ 2022-01-25 18:05  树下水月  阅读(567)  评论(1编辑  收藏  举报