private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}
else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
這段代碼看了幾遍,始終難以理解它的意思,誰能詳細解釋一下,base在這裡是什麼意思? base = base.length() == 0 ? "" : base + "/";這句是干什麼的?還有ZipEntry類是創建文件進入點, out.putNextEntry(new ZipEntry(base + "/"));究竟執行了什麼操作?
base是表示要壓縮的根目錄;
base = base.length() == 0 ? "" : base + "/";這句話表示如果指定了根目錄,就用指定的,否則就用默認的目錄;
out.putNextEntry(new ZipEntry(base + "/"));壓縮文件可以有多個文件層次,這個表示創建壓縮文件裡的文件夾。