java将文件打包zip
ZipUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.soc.cloud.groupAssetDataManage.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.soc.cloud.excel.util.DateUtil; public class ZipUtil { /** * * @param imgUrl 需要打包的路径集合 * @param zipFilePathExport 打包后zip所在的文件夹 * @param zipFileName 打包后zip文件名 * @return */ public static String toZip2(List<String> imgUrl,String zipFilePathExport,String zipFileName) { ZipOutputStream zos = null ; BufferedOutputStream out; try { out = new BufferedOutputStream( new FileOutputStream( new File(zipFilePathExport + "/" + zipFileName))); zos = new ZipOutputStream(out); // 将所有文件打包进zip包 for (String isfileUrl : imgUrl) { File isfile = new File(isfileUrl); String fileName = isfileUrl.substring(isfileUrl.lastIndexOf( "/" )+ 1 ); if (isfile.exists()) { InputStream in = new BufferedInputStream( new FileInputStream(isfile)); zos.putNextEntry( new ZipEntry(fileName)); byte [] b2 = new byte [ 1024 ]; int bytesRead2; while ((bytesRead2 = in.read(b2)) > 0 ) { zos.write(b2, 0 , bytesRead2); } in.close(); zos.closeEntry(); } } } catch (Exception e) { e.printStackTrace(); } finally { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } // 返回zip的路径(传回页面进行导出) return zipFilePathExport + "/" + zipFileName; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了