【发布时间】:2012-03-22 20:08:48
【问题描述】:
我在使用 Java 解压缩 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 ) 正在返回零字节,不知道究竟是为什么。
【问题讨论】:
-
你试过调试吗? in.read() 调用是否返回一些字节?
-
不要在循环内刷新。
标签: java zip compression