对hutool工具类进行的封装
依赖
cn.hutool hutool-all 5.8.15 compile
压缩代码
package com.myrc.web.util;import cn.hutool.core.collection.CollUtil;import cn.hutool.core.util.ZipUtil;import lombok.extern.slf4j.Slf4j;import javax.activation.MimetypesFileTypeMap;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.net.URLEncoder;import java.util.List;@Slf4j@SuppressWarnings("all")public class ZipFileUtils { public static void generateZip(File targetZipFile, List sourceFiles, boolean dirWithFlag) { if (CollUtil.isNotEmpty(sourceFiles)) { File[] fileArr = sourceFiles.toArray(new File[]{}); ZipUtil.zip(targetZipFile, dirWithFlag, fileArr); } } public static void downloadZip(File file, HttpServletResponse response) { OutputStream toClient = null; try { // 以流的形式下载文件。 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); toClient = new BufferedOutputStream(response.getOutputStream()); response.setCharacterEncoding("UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + file.getName()); toClient.write(buffer); toClient.flush(); } catch (Exception e) { log.error("下载zip压缩包过程发生异常:", e); } finally { if (toClient != null) { try { toClient.close(); } catch (IOException e) { log.error("zip包下载关流失败:", e); } } //删除改临时zip包(此zip包任何时候都不需要保留,因为源文件随时可以再次进行压缩生成zip包) file.delete(); } } public static void downloadAnyFile(File file, HttpServletResponse response) { FileInputStream fileInputStream = null; OutputStream outputStream = null; try { fileInputStream = new FileInputStream(file); // 清空response response.reset(); //防止文件名中文乱码 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"UTF-8")); //根据文件动态setContentType response.setContentType(new MimetypesFileTypeMap().getContentType(file) + ";charset=UTF-8"); outputStream = response.getOutputStream(); byte[] bytes = new byte[2048]; int len; while ((len = fileInputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }}
FileUtil 工具类补齐
public class FileUtil { public static List getAllFile(String dirFilePath) { if (StrUtil.isBlank(dirFilePath)) return null; return getAllFile(new File(dirFilePath)); } public static List getAllFile(File dirFile) { // 如果文件夹不存在或着不是文件夹,则返回 null if (Objects.isNull(dirFile) || !dirFile.exists() || dirFile.isFile()) return null; File[] childrenFiles = dirFile.listFiles(); if (Objects.isNull(childrenFiles) || childrenFiles.length == 0) return null; List files = new ArrayList<>(); for (File childFile : childrenFiles) { // 如果是文件,直接添加到结果集合 if (childFile.isFile()) { files.add(childFile); } //以下几行代码取消注释后可以将所有子文件夹里的文件也获取到列表里// else {// // 如果是文件夹,则将其内部文件添加进结果集合// List cFiles = getAllFile(childFile);// if (Objects.isNull(cFiles) || cFiles.isEmpty()) continue;// files.addAll(cFiles);// } } return files; }}
解压缩
zipUtil是hutool包下的工具类
File unzip = ZipUtil.unzip(zipPath, StandardCharsets.UTF_8);
使用:
//获取该目录下全部的文件List allFile = FileUtil.getAllFile(configPath);//对目标文件集 压缩ZipFileUtils.generateZip(new File(zipPath),allFile,true);
来源地址:https://blog.csdn.net/qq_43473129/article/details/129119088