Java操作Zip压缩⽂件遇到的编码问题
1、遇到的问题:在for循环操作zip⽂件(⽂件名包含中⽂)的过程中,报错如下:
Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
at java.util.String(ZipCoder.java:58)
at java.util.adLOC(ZipInputStream.java:297)
at java.util.NextEntry(ZipInputStream.java:121)
at com.forestfood.FileUntils.done(FileUntils.java:224)
黄毅清的微博at com.ActionZipFiles(FileUntils.java:196)
韩孝周
at com.forestfood.FileUntils.main(FileUntils.java:24)
2、查原因:追查报错代码⾏⾄  ZipCoder.java:58小拜年简谱
final class ZipCoder {if only you
String toString(byte[] ba, int length) {
CharsetDecoder cd = decoder().reset();
int len = (int)(length * cd.maxCharsPerByte());
char[] ca = new char[len];
if (len == 0)
return new String(ca);
// UTF-8 only for now. Other ArrayDeocder only handles// CodingErrorAction.REPLACE mode. ZipCoder uses// REPORT mode.if (isUTF8 && cd instanceo int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
安静的歌曲if (clen == -1)    // malformed throw new IllegalArgumentException("MALFORMED");
return new String(ca, 0, clen);
}
可以发现,如果不指定编码的时候,默认是UTF-8,在解码的时候就会发⽣错误。
任韵淇
3、解决⽅法:在创建zip的时候,指定编码
FileInputStream fileInputStream = new FileInputStream(zipPath);
Charset gbk = Charset.forName("gbk");
ZipInputStream zin = new ZipInputStream(fileInputStream,gbk);
ZipEntry entry = NextEntry();