public static void testZipToUnzip(ZipFile zipfile,File file){
//将一个压缩文件zipfile(可以包含文件)解压到file文件中
ZipEntry ze=null;
File outfile;
InputStream zis=null;
FileOutputStream fos=null;
Enumeration e=zipfile.getEntries();
while(e.hasMoreElements()){
ze=(ZipEntry) e.nextElement();
// System.out.println(ze.getName());
outfile=new File(file+File.separator+ze.getName());
//if(ze.toString().length()!=(ze.getName()).lastIndexOf('/')+1)
//这句话的作用:如果ze不是一个目录则执行下面的语句,也就是创建输出文件并且将输入流输出到输出流中。
//上面的if语句作用和该语句作用一样
if(!ze.isDirectory()){
try {
//System.out.println(ze.getName());
zis=zipfile.getInputStream(ze);
//System.out.println(ze.getSize());
//System.out.println(outfile.getPath());
if(!outfile.exists())
//如果输出文件不存在,那么创建输出文件的父目录
outfile.getParentFile().mkdirs();
fos=new FileOutputStream(outfile);
byte[] b=new byte[1024];
int len;
while((len=zis.read(b))>0){
fos.write(b, 0, len);
}
fos.close();
zis.close();
} catch (ZipException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Zip文件的内容
最新推荐文章于 2024-04-10 14:21:32 发布