今天,要做支付宝对账,是从支付宝提供的sftp地址上面,读取支付宝生成的zip对账文件进行读取里面的文件,但是后来发现用zipInputStream读取的时候,一直报错java.lang.IllegalArgumentException这个异常。后来经过debug发现,ZipEntry竟然为空。最终网上搜了下资料,说是用ant.jar来解决,特此在这里记录下解决问题的过程:
方法一:使用ant.jar
首先加入ant的pom
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.6</version>
</dependency>
然后写个测试类:
@org.junit.Test
public void test2() throws IOException {
ZipFile zip = new ZipFile(new File("D:/2088011445567254-20160114.zip"),"GBK");
Enumeration<org.apache.tools.zip.ZipEntry> enumeration = zip.getEntries();
while (enumeration.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEntry = enumeration.nextElement();
System.out.println(zipEntry.getName());
}
}
控制台正常的解析出结果:
方法二:使用commons-compress.jar
加入pom依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.10</version>
</dependency>
方法:
@org.junit.Test
public void test2() throws IOException {
ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(new FileInputStream(new File("D:/2088011445567254-20160114.zip")),"gbk");
ZipArchiveEntry zipArchiveEntry = null;
while ((zipArchiveEntry = zipArchiveInputStream.getNextZipEntry()) != null) {
System.out.println(zipArchiveEntry.getName());
}
结果: