我从服务器收到一个zip文件,必须进行处理。我可以手动解压缩它。所以我认为zip文件没有损坏。
下面的代码从Zip文件中读取文件而不解压缩。
import java.io.File;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipFileReader {
public static void main(String[] args) throws Exception{
File file = new File("/Users/wire/data.zip");
ZipFile zipFile = new ZipFile(file.getPath());//Getting error here
for (Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
System.out.println(entry.getName());
}
}
}
抛出以下错误:
Exception in thread "main" java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:155)
at java.util.zip.ZipFile.<init>(ZipFile.java:126)
at com.log.ZipFileReader.main(ZipFileReader.java:11)
但如果我手动解压缩并将其拉回,上面的代码没有问题,下面是输出:
data/
data/file/
data/file/log.txt
__MACOSX/
__MACOSX/data/
__MACOSX/data/file/
__MACOSX/data/file/._log.txt
注意:我从服务器大小收到的zip文件是5MB。但是一旦我解压缩并将其拉回,新的zip文件大小就变成了8MB。
加了:
Apples-MacBook-Pro:~ test$ unzip -v data.zip
Archive: data.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of data.zip or
data.zip.zip, and cannot find data.zip.ZIP, period.