简单java解压缩问题

来源:百度知道 编辑:UC知道 时间:2024/07/01 01:23:13
今天自学到解压缩,发现点问题,先看代码:
public class Decompressing {
public static void main(String[] args) {
ZipInputStream zin;
try{
zin=new ZipInputStream(new FileInputStream("e:/hello.zip"));
ZipEntry entry=zin.getNextEntry();
while(((entry=zin.getNextEntry())!=null)&&!entry.isDirectory()){
File file=new File(entry.getName());
if(!file.exists()){
file.createNewFile();

}
zin.closeEntry();
System.out.println("ok"+entry.getName());
}
zin.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

e盘的hello。ZIP压缩文件已经存在,我调用上面的程序解压缩,发现,必须要在E盘创建好hello文件夹才能运行,如果不存在hello文件夹代码就无法执行,这是为什么?
还有就是我想把解压缩的文件放到制定的文件夹里,即使用createzipentry()方法,以上程序该怎么改?

如果前辈可以,给我讲下while{}循环的意思,通俗点谢谢!

既然目录的问题那你可以在输出流输出之前创建目录撒,
你先创建一个File对象
然后测试他是否为一个目录,不是就创建目录
如你hello.zip
File f=new File("E:\\hello");
if(!f.isDirectory()){
f.mkdirs();
}

如果事先没有hello.zip则FileInputStream初始化失败,程序自然不能运行~