ZipInputStream 指定字符集?-灵析社区

劳资最帅

new ZipInputStream(new FileInputStream("test.zip"), StandardCharsets.UTF_8) 没有用呢,中文的文件、文件夹仍会报错(`malformed input off : 1, length : 1`)。 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/f462552f1c565fa5afadb2a367327bde.png) ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/0753960bfdf07b4b94bf17f0452cfdc2.png) 这里都有注释的嘛,为什么会这样?

阅读量:15

点赞量:0

问AI
你把编码换成GBK就可以了: FileInputStream input = new FileInputStream(targetPath); ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK")); 原因是操作系统平台的差异,导致zip压缩包的编码格式不同,windows默认使用GB2312格式,mac和linux默认使用UTF-8格式,你指定UTF-8对于GB2312的字符转化没有起作用,而GBK是兼容GB2312的。 如果你想你的代码无视操作系统差异,可以使用 "Apache Commons Compress" 这个包的压缩/解压缩方法试试: "https://zhuanlan.zhihu.com/p/389762356" (https://link.segmentfault.com/?enc=p4KM425hYxSWYH%2FDrPg2ew%3D%3D.qYUzeZ7k0v6MeaMOLF%2F68g237bTzPtwMIkrgZAtXBPzA2IBIYLxmBtqnK8yLBFJr)