import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Slf4j public class FileZipUtil { private static final String utf8 = "UTF-8"; /** * 下载zip文件 * @param srcfile * @param zipFileName * @param response */ public static void createZipByBytes(List<Map<String, Object>> srcfile, String zipFileName, HttpServletResponse response) { byte[] buf = new byte[1024]; BufferedOutputStream bos; InputStream in = null; ZipOutputStream out = null; try { bos = new BufferedOutputStream(response.getOutputStream()); response.reset(); response.setCharacterEncoding("utf-8"); response.addHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Expose-Headers", "*"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, utf8)); response.setContentType("application/octet-stream;charset=UTF-8"); out = new ZipOutputStream(bos); for (int i = 0; i < srcfile.size(); i++) { byte[] bytes = (byte[]) srcfile.get(i).get("content"); in = new ByteArrayInputStream(bytes); out.putNextEntry(new ZipEntry(srcfile.get(i).get("fileName").toString())); int len; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } } out.close(); bos.close(); log.info("压缩完成"); } catch (Exception e) { log.error("zip文件打包异常{}",e.getMessage()); throw new BusinessException("zip文件打包异常"); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 下载zip文件 * @param srcfile * @param zipFileName * @param response */ public static void createZipByFiles(List<File> srcfile,String zipFileName,HttpServletResponse response) { byte[] buf = new byte[1024]; // 获取输出流 BufferedOutputStream bos; FileInputStream in = null; ZipOutputStream out = null; try { bos = new BufferedOutputStream(response.getOutputStream()); response.reset(); response.setCharacterEncoding("utf-8"); response.addHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Expose-Headers", "*"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, utf8)); response.setContentType("application/octet-stream;charset=UTF-8"); out = new ZipOutputStream(bos); for (int i = 0; i < srcfile.size(); i++) { in = new FileInputStream(srcfile.get(i)); out.putNextEntry(new ZipEntry(srcfile.get(i).getName())); int len; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } } out.close(); bos.close(); log.info("压缩完成"); } catch (Exception e) { log.error("zip文件打包异常{}",e.getMessage()); throw new BusinessException("zip文件打包异常"); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2019-11-10 源码解析之mybatis