利用ant的ZipOutputStream进行文件合成zip进行下载

直接看代码

     List<Map<String, String>> filePaths = new ArrayList<>();
for (LegislativePlanDraftFile legislativePlanDraftFile : list) {
Map<String, String> map = new HashMap<>();
map.put("filePath", legislativePlanDraftFile.getFilePath());
map.put("fileName", legislativePlanDraftFile.getFileName());

filePaths.add(map);
}
String zipName = "附件" + ".zip";
String zipPath = "/ms-files/" + format.format(new Date()) + "/" + zipName;
// String zipPath = "F:\\base-log\\" + zipName; // 自己项目保存文件的位置
CompressUtil.compress(filePaths, zipPath, false);
File pocZipFile = new File(zipPath);
CompressUtil.downloadZip(response, zipName, pocZipFile);

 

 

 先把查询出来的文件路径存入map集合中,进行循环读取,在存入

工具类

/**

/**
* zip文件下载工具类
*/
public class CompressUtil {

/**
* 生成zip压缩文件
* @param filePaths
* @param zipFilePath
* @param keepDirStructure
*/
public static void compress(List<Map<String, String>> filePaths, String zipFilePath, Boolean keepDirStructure) {
byte[] buf = new byte[1024];
File zipFile = new File(zipFilePath);
try {
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()));
zos.setEncoding("UTF-8"); // 有可能出现乱码的情况,手动设置编码格式
for (int i = 0; i < filePaths.size(); i++) {
String relativePath = filePaths.get(i).get("filePath");
String relativeName = filePaths.get(i).get("fileName");
if (StringUtils.isEmpty(relativePath)) {
continue;
}
File sourceFile = new File(relativePath);
if (!sourceFile.exists()) {
continue;
}
FileInputStream fis = new FileInputStream(sourceFile);
if (keepDirStructure != null && keepDirStructure) {
zos.putNextEntry(new ZipEntry(relativePath));
} else {
zos.putNextEntry(new ZipEntry((i + 1) + "_" + relativeName));
}
int len;
while ((len = fis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
}
zos.close();
if (!zipFile.exists()) {
zipFile.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 下载zip
*
* @param response
* @param zipName 浏览器header中zip名称
* @param zipFile zipFile文件
*/
public static void downloadZip(HttpServletResponse response, String zipName, File zipFile) {
//下载文件
try {
        // 设置自己需要的返回头信息,可以跟前端商量
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Content-Disposition", "attachment; filename=" +
URLEncoder.encode(zipName, "UTF-8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("Last-Modified", new Date().toString());
response.setHeader("ETag", String.valueOf(System.currentTimeMillis()));
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-zip-compressed"); // 主要是返回的格式,要给到前端
ServletOutputStream out = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(zipFile);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
out.flush();
}
out.close();
fis.close();
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
posted @   itMuzi  阅读(152)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示