本文只针对压缩文件夹(不用于压缩文件,若需要,自行更改)
public class ZipTool {
File f0; //被压缩的目标
File f1; //压缩后的目录及文件名
public ZipTool(File f0, File f1) {
this.f0 = f0;
this.f1 = f1;
start();
}
public void start() {
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f1))) {
File[] flist = f0.listFiles();//对输入目录进行查看
for (File o : flist) {
writeFile(o, out);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeFile(File f, ZipOutputStream out) {
try {
if (judgeFile(f)) {
File[] flists = f.listFiles();//判断输入的文件夹是否有文件
if (flists.length == 0) {
String s = f.toString().substring(3);
out.putNextEntry(new ZipEntry(s + " " + "/"));//空文件仅仅创建文件夹
} else {//非空文件则遍历后创建文件
for (File oo : flists) {
writeFile(oo, out); //递归方法
}
}
} else {
InputStream in = new BufferedInputStream(new FileInputStream(f));
int len;
byte[] b = new byte[8192];
String s = f.toString().substring(3);
out.putNextEntry(new ZipEntry(s));
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}//写文件
public boolean judgeFile(File f) {
if (f.isDirectory()) {
return true;//是文件夹就返回
} else {
return false;//不是就不返回
}
}//对文件夹处理
}
已知BUG:对于空文件夹的压缩,压缩后名字与原文件夹不相符
原因:因不知某因无法直接压缩空文件夹,故给空文件明加了个空字符串
流程:放目录→判断是否为文件夹→是→判断是否为文件夹→是 创建空文件夹
⬇否 否⬇
直接创建对应文件 递归到最开始