2、查找原因:追查报错代码行至 ZipCoder.java:58
final class ZipCoder { 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 instanceof ArrayDecoder) { int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca); if (clen == -1) // malformedthrow 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 = zin.getNextEntry();