JAVA解压zip包中文乱码
来源:互联网 发布:什么是风险管理知乎 编辑:程序博客网 时间:2024/04/30 23:01
最近项目需要解析接口过来的日志文件,日志文件采用zip打包方式传递过来,zip包的内的文件名包含中文。
刚开始采用的java.util下的zip包进行解压,发现路径中文乱码,代码如下:
/** * 解压文件到指定目录 * * @param zipFile zip文件 * @param descDir 输出目录 * @author lee */@SuppressWarnings("rawtypes")public static void unZipFiles(File zipFile, String descDir)throws IOException {File pathFile = new File(descDir);if (!pathFile.exists()) {pathFile.mkdirs();}ZipFile zip = new ZipFile(zipFile);for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {ZipEntry entry = (ZipEntry) entries.nextElement();String zipEntryName = entry.getName();InputStream in = zip.getInputStream(entry);String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");;// 判断路径是否存在,不存在则创建文件路径File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));if (!file.exists()) {file.mkdirs();}// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (new File(outPath).isDirectory()) {continue;}// 输出文件路径信息System.out.println(outPath);OutputStream out = new FileOutputStream(outPath);byte[] buf1 = new byte[1024];int len;while ((len = in.read(buf1)) > 0) {out.write(buf1, 0, len);}in.close();out.close();}zip.close();}
在网上查阅相关资料,java.uil下的zip处理类不包含中文名字的兼容,需要采用ant包下的apache.tools工具进行解析,代码如下:
import org.apache.commons.io.FileUtils;import org.apache.commons.io.IOUtils;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import org.xml.sax.SAXException;import java.io.*;import java.util.*;public class ZipUtils { /** * 解析附件zip包 * @param unZipFileName 解压的zip文件 * @param outputDirectory 输出目录 * @throws StrategyException */ public static void unZip(String unZipFileName,String outputDirectory) throws StrategyException { FileOutputStream fileOut = null; InputStream inputStream = null; ZipFile zipFile = null; int readedBytes; try{ //创建输出目录 File outputDirFile = new File(outputDirectory); if(!outputDirFile.exists()){ outputDirFile.mkdir(); } if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){ zipFile = new ZipFile(unZipFileName,"GBK"); }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){ zipFile = new ZipFile(unZipFileName,"UTF-8"); } for(Enumeration entries = zipFile.getEntries();entries.hasMoreElements();){ ZipEntry entry = (ZipEntry)entries.nextElement(); File f = new File(outputDirectory + File.separator + entry.getName()); f.createNewFile(); inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(f); byte[] buf = new byte[1024]; while(( readedBytes = inputStream.read(buf) ) > 0){ fileOut.write(buf , 0 , readedBytes ); } } }catch(Exception e){ e.printStackTrace(); throw new StrategyException("解析附件失败,请检查压缩包是否损坏!"); }finally { IOUtils.closeQuietly(fileOut); IOUtils.closeQuietly(inputStream); } }}
0 0
- JAVA解压zip包中文乱码
- Ubuntu16.04解压zip包中文乱码
- PHP解压ZIP压缩包,解决中文乱码问题
- linux解压中文zip乱码
- java解压zip包
- java 解压zip包
- 完美解决java生成或解压zip包乱码问题
- Java实现Zip压缩与解压(解决中文乱码问题)
- Java中ZIP压缩与解压--中文文件名乱码解决办法
- Java实现Zip压缩与解压(解决中文乱码问题)
- java解压zip或rar压缩文件(有源码和jar包以及对中文乱码的处理方法)
- 解压zip压缩包(JAVA)
- java解压Zip压缩包
- java解压zip压缩包
- zip压缩包解压乱码问题
- linux中解压zip 中文乱码
- Linux解压zip中文乱码(绝对管用)
- linux中解压zip 中文乱码
- Matlab的选择结构
- iOS 下的文件操作
- 【转】spring boot 入门
- python基于pymysql连接数据库
- REST风格
- JAVA解压zip包中文乱码
- OKHttp框架的使用
- Hadoop HDFS Shell命令
- 七款嵌入式Linux操作系统简介
- 在win7的idea访问centos部署的hadoop2.7.1问题集锦
- 【转载】Linus下的/etc/hosts文件详解
- 【转载】MapReduce模型
- Leetcode69. Sqrt(x)
- 迭代开发