利用压缩流解压文件夹
@Test
public void zipInputStreamDemo()throws Exception{
//利用压缩流解压文件夹,注意java只识别zip
File src = new File("D:/a.zip");
File dist = new File("D:/");
unzip(src,dist);
}
public static void unzip(File src,File dest)throws Exception{
//创建一个压缩输入流
ZipInputStream zip = new ZipInputStream(new FileInputStream(src));
ZipEntry entry;
//利用循环获取每一个ZipEntry对象[这个对象能获取压缩包下面所有的文件夹及文件]
while ((entry=zip.getNextEntry())!=null){
//判断这个ZipEntry是否是一个文件夹,如果是则创建这个文件夹,如果不是则将这个文件解压出来。
if(entry.isDirectory()){
File file = new File(dest,entry.toString());
file.mkdir();
}
else
{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dest,entry.toString())));
byte[] bytes = new byte[1024];
int len = 0;
while ((len= zip.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.flush();
bos.close();
zip.closeEntry();
}
}
zip.close();
}
标签:src,java,zip,压缩,文件夹,File,entry,new
From: https://www.cnblogs.com/-xyk/p/17189301.html