想通过ZipInStream类将压缩文件解压到指定的文件夹中


想通过ZipInStream类将压缩文件解压到指定的文件夹中
源程序是:
import java.io.*;
import java.util.zip.*;

public class Decompressing { // 创建文件
public static void main(String[] temp) {
ZipInputStream zin; // 创建ZipInputStream对象
try { // try语句捕获可能发生的异常
zin = new ZipInputStream(new FileInputStream("F:/hello.zip"));
// 实例化对象,指明要进行解压的文件
ZipEntry entry = zin.getNextEntry(); // 获取下一个ZipEntry
while (((entry = zin.getNextEntry()) != null)
&& !entry.isDirectory()) {
// 如果entry不为空,并不在同一目录下
File file = new File("F:\" + entry.getName()); // 获取文件目录
System.out.println(file);
if (!file.exists()) { // 如果该文件不存在
file.mkdirs();// 创建文件所在文件夹
file.createNewFile(); // 创建文件
}
zin.closeEntry(); // 关闭当前entry
System.out.println(entry.getName() + "解压成功");
}
zin.close(); // 关闭流
} catch (Exception e) {
e.printStackTrace();
}
}
}
可是程序运行不了,有以下异常:
java.lang.IllegalArgumentException: MALFORMED
at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:300)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122)
at 练习.Decompressing.main(Decompressing.java:48)

F盘里面有hello.zip
希望大神帮忙解答一下,特别是那个entry和getNextEntry到底是什么意思,没弄懂啊



相关链接

java IO

Agerter 9 years, 9 months ago

Your Answer