== start ==
1.记录打包zip导出 主要代码:
private void exportZip(HttpServletReponse reponse,String parentPath){
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
File file = new File(parentPath);//源文件路径
this.compress(file, zipOutputStream, “materialReport”);//文件,zip流,解压的文件名
zipOutputStream.close();//用完记得完毕
}
private void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception {
byte[] buf = new byte[2 * 1024];
if (sourceFile.isFile()) {
//文件写入
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();//每次往里面的放文件的之后记得关闭
in.close();//流也关闭
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
//文件夹生成目录
zos.putNextEntry(new ZipEntry(name + “/”));
zos.closeEntry();
} else {
for (File file : listFiles) {
//递归调用知道没有下一级文件或者目录
this.compress(file, zos, name + “/” + file.getName(), sourceUrlList);
}
}
}
}
== end ==