工作中遇得到了批量导出问题,要求导出zip
大致步骤:
1、现将文件输出到服务器上
2、对文件加进行加压(zip)
3、导出zip文件
4、删除服务器上生成文件
注:如果多人同时操作可以对文件夹名字进行操作,比如将文件夹内层名字设为时间戳,删除的时候就只删除内层文件
zip工具类:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
/**
* 完成的结果文件--输出的压缩文件
*/
File targetFile;
public ZipUtil() {
}
public ZipUtil(File target) {
targetFile = target;
if (targetFile.exists())
targetFile.delete();
}
/**
* 压缩文件
* @param srcfile
*/
public void zipFiles(File srcfile) {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(targetFile));
if (srcfile.isFile()) {
zipFile(srcfile, out, "");
} else {
File[] list = srcfile.listFiles();
for (int i = 0; i < list.length; i++) {
compress(list[i], out, "");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 指定多个文件夹压缩
* @param files
*/
public void zipFiles(File[] files) {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(targetFile));
File[] list = files;
for (int i = 0; i < list.length; i++) {
compress(list[i], out, "");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 压缩文件夹里的文件 起初不知道是文件还是文件夹--- 统一调用该方法
* @param file
* @param out
* @param basedir
*/
private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
this.zipDirectory(file, out, basedir);
} else {
this.zipFile(file, out, basedir);
}
}
/**
* 压缩单个文件
*
* @param srcfile
*/
public void zipFile(File srcfile, ZipOutputStream out, String basedir) {
if (!srcfile.exists())
return;
if (srcfile.getAbsolutePath().equals(targetFile.getAbsolutePath())) {// 排除自己
return;
}
byte[] buf = new byte[1024];
FileInputStream in = null;
try {
int len;
in = new FileInputStream(srcfile);
out.putNextEntry(new ZipEntry(basedir + srcfile.getName()));
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.closeEntry();
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 压缩文件夹
* @param dir
* @param out
* @param basedir
*/
public void zipDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/***
* 删除指定文件夹下所有文件
* @param path 文件夹完整绝对路径
* @return
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
flag = true;
}
}
return flag;
}
/**
* @param args
*/
public static void main(String[] args) {
//压缩文件名(加后缀.zip)
String zipName="xxxx.zip";
new ZipUtil(new File("压缩文件路径", zipName)).zipFiles(f);
}
}
业务代码:
String outputUrl = "D:\\导出路径\\";
File fileDir = new File(outputUrl);
// 创建文件夹
if (!fileDir.exists() && !fileDir.isDirectory()) {
fileDir.mkdirs();
}
for (Map<String, String> map : sqlList) {
/**
* 业务代码(文件输出到服务器)
* /
}
//定义压缩文件名(根据自己需要设置,我这设置的路径上的文件夹名字)
String zipUrl = fileDir.getName() + ".zip";
// 压缩文件夹
new ZipUtil(new File(outputUrl, zipUrl)).zipFiles(fileDir);
//定义压缩包存放路径(根据自己需要设置)
File f = new File(outputUrl + fileDir.getName() + ".zip");
// 编码zip文件名
String zipName = java.net.URLEncoder.encode(zipUrl, "utf-8");
/*
* 下载zip文件
*/
InputStream ins = new BufferedInputStream(
new FileInputStream(f));
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
response.reset();
response.addHeader("Content-Disposition",
"attachment;filename=" + zipName);
response.addHeader("Content-Length", "" + f.length());
OutputStream out = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/octet-stream");
out.write(buffer);
out.flush();
out.close();
// 清空下载文件夹下内容
ZipUtil.delAllFile(outputUrl);