在调用JDK自带的zipfile读取压缩包文件的时候,出现了以下错误:
SjXcmcFeignClient:124 - BaseFeignClient 拦截到【未知系统异常】Exception异常:
java.lang.IllegalArgumentException: MALFORMED[1]
at java.util.zip.ZipCoder.toString(ZipCoder.java:65)
at java.util.zip.ZipFile.getZipEntry(ZipFile.java:583)
at java.util.zip.ZipFile.access$900(ZipFile.java:60)
at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:539)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:514)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:495)
at com.xcwlkj.ksyw.web.KsjhglSjXcmcFeignClient.updatekszp(KsjhglSjXcmcFeignClient.java:962)
......
后来经过检查,发现压缩包内有一个文件的名字带有中文,解析压缩包中,读取中文文件导致报错,然后查看了Zipfile的构造方法,发现还有一个设置字符编码的参数
public ZipFile(String name, Charset charset) throws IOException
{
this(new File(name), OPEN_READ, charset);
}
于是尝试设置zip的字符编码为GBK格式,如下:
ZipFile zipFile=new ZipFile(zipPath,Charset.forName("GBK"));
代码重新编译后执行,问题得到解决。
来源地址:https://blog.csdn.net/yasashii/article/details/128919851