使用ZipInputStream对zip类型的文件进行解压
package a04dome3.unzip;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class fileUnzip {
public static void main(String[] args) throws IOException {
//创建对象表示目标文件
File src = new File("D:\\aaa.zip");
//创建对象表示目的文件
File dest = new File("D:\\");
unzip(src,dest);
}
private static void unzip(File src, File dest) throws IOException {
//创建压缩文件的对象表示需要压缩的文件
ZipInputStream zis = new ZipInputStream(new FileInputStream(src));
//创建临时变量用来存储每个文件或者文件夹
ZipEntry entry;
//读取zip压缩包里面的文件或者文件夹
while((entry = zis.getNextEntry()) != null){
if(entry.isDirectory()){
//是文件夹
//在目的目录下创建文件夹
File file = new File(dest,entry.toString());
file.mkdirs();
}else{
//是文件
//在目的文件下创建文件
FileOutputStream fos = new FileOutputStream(new File(dest,entry.toString()));
//一个字节的写
int b;
while((b = zis.read()) != -1){
fos.write(b);
}
//关流
fos.close();
zis.closeEntry();
}
}
zis.close();
}
}