java打包导出(zip格式)

时间:2025-04-22 22:26:44
public class ZipUtil { private static final String ZIP_NAME = "要生成的压缩包名字"; /** * 压缩多个文件成一个zip文件 * * <p>Title: compressionFile</p> * <p>Description: </p> * @time 下午1:48:38 * @param srcfile 源文件列表 * @param zipfile 压缩后的文件 */ public static void compressionFile(File[] srcfile, File zipfile) { byte[] buffer = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); for (int i = 0; i < ; i++) { FileInputStream fileInputStream = new FileInputStream(srcfile[i]); (new ZipEntry(srcfile[i].getName())); int length; while ((length = (buffer)) > 0) { (buffer, 0, length); } (); (); } (); } catch (Exception e) { (); } } /** * 解压缩 * * <p>Title: uncompression</p> * <p>Description: </p> * @time 下午1:36:22 * @param zipfile 需要解压缩的文件 * @param descDir 解压后的目标目录 */ @SuppressWarnings({ "resource", "rawtypes" }) public static void unompressionFile(File zipfile, String descDir) { try { ZipFile zipFile = new ZipFile(zipfile); for (Enumeration entries = (); ();) { ZipEntry entry = (ZipEntry) (); String zipEntryName = (); InputStream in = (entry); OutputStream out = new FileOutputStream(descDir + zipEntryName); byte[] buf1 = new byte[1024]; int len; while ((len = (buf1)) > 0) { (buf1, 0, len); } (); (); } } catch (Exception e) { (); } } /** * 调用执行压缩 * * <p>Title: excuteCompression</p> * <p>Description: </p> * @time 下午4:22:38 * @param filename压缩包名字 * @throws IOException */ public static void excuteCompression(HttpServletRequest request, HttpServletResponse response, String filename, String pdfPath) throws IOException { String zipPath = ().getServletContext().getRealPath(ZIP_NAME); //创建文件夹 File folder = new File(zipPath); if (!()) { (); } (); String filePath = pdfPath; File file = new File(filePath); // 获取文件夹下所有文件信息 File[] srcfile = (); // 压缩包存放位置 File zipfile = new File(zipPath + "/" + filename + ".zip"); // 调用压缩方法 (srcfile, zipfile); // 删除生成的文件夹和文件 String downPath = zipPath + "/" + filename + ".zip"; (downPath, response); //删除源文件夹及其内容 (file); //删除下载完的压缩包存储临时文件夹及其内容 File zfile = new File(zipPath); (zfile); } /** * <p>Title: excuteUncompression</p> * <p>Description: </p> * @time 上午9:50:26 * @param filePath * 这里需要两个路径 * 首先传入一个要解压的文件所在路径 * 然后定义解压之后的存储路径 */ public static void excuteUncompression(String filePath) { //获取桌面绝对路径 File desktopDir = ().getHomeDirectory(); String desktopPath = (); //需要解压缩的文件 File file = new File(filePath); //解压后默认存放在桌面 String dir = desktopPath; (file, dir); } /** * 下载服务器下生成的文件 * * <p>Title: download</p> * <p>Description: </p> * @time 下午2:51:34 * @param path * @param response */ public static void download(String path, HttpServletResponse response) { InputStream in = null; OutputStream out = null; try { File file = new File(path); /* 读取要下载的文件,保存到文件输入流 */ in = new FileInputStream(file); /* 设置响应头,控制浏览器下载该文件 */ ("UTF-8"); ("application/octet-stream"); ("Content-disposition", "attachment; filename=" + new String(().getBytes("UTF-8"), "ISO8859-1")); /* 创建缓冲输出流 */ out = new BufferedOutputStream(()); /* 定义缓冲区大小,开始读写 */ byte buffer[] = new byte[2048]; int len = 0; while ((len = (buffer)) > 0) { (buffer, 0, len); } /* 刷新缓冲区,写出 */ (); } catch (IOException ex) { (); } } /** * 删除文件夹及其下面的所有文件 * * <p>Title: deleteAll</p> * <p>Description: </p> * @time 下午4:09:03 * @param file */ public static void deleteAll(File file) { if (() || ().length == 0) { (); } else { for (File f : ()) { // 递归删除文件夹下的每一个文件 deleteAll(f); } // 删除文件夹 (); } } }