我在使用Java解压ZIP文件时遇到了问题。以下是我的方法。
一旦文件被解压缩,文件结构是正确的,这意味着ZIP文件内的目录是正确的,但输出的文件长度为零。
我已经检查了ZIP文件以查看是否正确压缩,所有都正确。
如果有人发现我漏掉了什么,请告诉我。
一旦文件被解压缩,文件结构是正确的,这意味着ZIP文件内的目录是正确的,但输出的文件长度为零。
我已经检查了ZIP文件以查看是否正确压缩,所有都正确。
如果有人发现我漏掉了什么,请告诉我。
public static void unzip ( File zipfile, File directory ) throws IOException {
ZipFile zip = new ZipFile ( zipfile );
Enumeration<? extends ZipEntry> entries = zip.entries ();
while ( entries.hasMoreElements () ) {
ZipEntry entry = entries.nextElement ();
File file = new File ( directory, entry.getName () );
if ( entry.isDirectory () ) {
file.mkdirs ();
}
else {
file.getParentFile ().mkdirs ();
ZipInputStream in = new ZipInputStream ( zip.getInputStream ( entry ) );
OutputStream out = new FileOutputStream ( file );
byte[] buffer = new byte[4096];
int readed = 0;
while ( ( readed = in.read ( buffer ) ) > 0 ) {
out.write ( buffer, 0, readed );
out.flush ();
}
out.close ();
in.close ();
}
}
zip.close ();
}
还有更多内容... 显然,方法getInputStream(entry)返回了零字节,不知道具体原因。