/**
文件压缩
*/
private static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf=new byte[1024];
ZipOutputStream out=null;
try {
out=new ZipOutputStream(new FileOutputStream(
zipfile));
for (int i=0; i < srcfile.length; i++) {
FileInputStream in=new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
int len;
while ((len=in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void test(String zipName){
File srcfile[]=new File[5];//数量可以通过本身的业务逻辑获取
for (int i=0; i < 5; i++) { //这里是在模拟添加文件 修改为自己要压缩的文件
srcfile[i]=new File("file"+i);
}
String zipname=zipName + ".zip";
File zip=new File(zipname);// 压缩文件
if (zip.exists()) {
zip.createNewFile();
}
ZipFiles(srcfile, zip);
}
我这边用到的是下载excel,因为数据量太大,弄的多线程生成excel,每个excel中有5000条数据,全部生成完成后将这些excel打包成压缩文件提供前端下载压缩文件。