PHP 使用 ZipArchive 解压避免乱码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $filePath : 压缩包路径 ../123.zip $path :要解压的目录 ../unzip/ public function newUnzip( $filePath , $path ) { $zip = new \ZipArchive(); if ( $zip ->open( $filePath ) === true) { // 创建要解压的目录 // 获取解压的文件数组 $fileNum = $zip ->numFiles; for ( $i = 0; $i < $fileNum ; $i ++) { // 处理每一个文件 $statInfo = $zip ->statIndex( $i , 64); // 获取文件的名称的编码格式 $info = explode ( '/' , $statInfo [ 'name' ]); foreach ( $info as $key => $val ) { $encoding = mb_detect_encoding( $val , [ 'UTF-8' , 'GBK' , 'BIG5' , 'CP936' ]); $info [ $key ] = iconv( $encoding , 'UTF-8//IGNORE' , $val ); } $statInfo [ 'name' ] = implode( '/' , $info ); mkdir ( $path , 0777, true); if ( strpos ( $statInfo [ 'name' ], '/' ) !== false) { $mkdFile = explode ( '/' , $statInfo [ 'name' ]); if ( count ( $mkdFile ) == 1) { $namePath = $mkdFile [0]; } else { $namePath = $mkdFile [ count ( $mkdFile ) - 1]; } //获取zip单个文件数据 $fp = fopen ( 'zip://' . $filePath . '#' . $zip ->getNameIndex( $i ), 'rb' ); $create = true; while (! feof ( $fp )) { //fread 获取文件数据流 保存到本地 file_put_contents ( $path . DIRECTORY_SEPARATOR . $namePath , fread ( $fp , 9999999), $create ? 0 : FILE_APPEND); $create = false; } } else { if (! copy ( 'zip://' . $filePath . '#' . $zip ->getNameIndex( $i ), $path . '/' . $statInfo [ 'name' ])) { return false; } } } } $zip ->close(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2018-05-31 tp5后台同步更新配置文件
2018-05-31 jquery导航栏高亮(二级菜单点击一级保持高亮)