ZipUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.ruoyi.common.utils.document;
  2. import javax.servlet.http.HttpServletResponse;
  3. import java.io.*;
  4. import java.net.URLEncoder;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipOutputStream;
  9. /**
  10. * 压缩工具Util
  11. */
  12. public class ZipUtil {
  13. /**
  14. * 创建zip文件并用流方式返回浏览器下载
  15. *
  16. * @param filePath 需压缩的文件路径
  17. * @param folder 文件上层目录,用于区分多次下载
  18. * @param zipName zip压缩包文件名
  19. * @param fileNames 目录下文件绝对路径集合
  20. * @param response
  21. * @throws Exception
  22. */
  23. public static void createZip(String filePath, String folder, String zipName, List<String> fileNames, HttpServletResponse response) throws Exception {
  24. OutputStream out = null;
  25. ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
  26. List<File> fileList = new ArrayList<>();
  27. for (String fileName : fileNames) {
  28. File file = new File(filePath + "\\" + folder + "\\" + fileName);
  29. fileList.add(file);
  30. }
  31. String zip = filePath + folder + "/" + zipName;
  32. try {
  33. File file = new File(filePath + "/" + folder);
  34. if (!file.isDirectory()) {
  35. file.mkdir();
  36. }
  37. File zipFile = new File(zip);
  38. if (!zipFile.exists()) {
  39. zipFile.createNewFile();
  40. }
  41. // 将package.zip的File对象传到toZip对象中
  42. toZip(fileList, zipFile);
  43. //把指定文件提取成流
  44. FileInputStream fis = new FileInputStream(zip);
  45. // 设置响应消息头,告诉浏览器当前响应是一个下载文件
  46. response.setContentType("application/x-msdownload");
  47. // 告诉浏览器,当前响应数据要求用户干预保存到文件中,以及文件名是什么 如果文件名有中文,必须URL编码
  48. String fileName = URLEncoder.encode(zipName, "UTF-8");
  49. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  50. out = response.getOutputStream();
  51. byte[] b = new byte[1000];
  52. int n;
  53. while ((n = fis.read(b)) != -1) {
  54. baos.write(b, 0, n);
  55. }
  56. fis.close();
  57. baos.writeTo(out);
  58. baos.close();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. throw new Exception("导出失败:" + e.getMessage());
  62. } finally {
  63. if (baos != null) {
  64. baos.close();
  65. }
  66. if (out != null) {
  67. out.close();
  68. }
  69. }
  70. }
  71. /**
  72. * 把文件集合打成zip压缩包
  73. *
  74. * @param srcFiles 压缩文件集合
  75. * @param zipFile zip文件名
  76. * @throws RuntimeException 异常
  77. */
  78. public static void toZip(List<File> srcFiles, File zipFile) throws IOException {
  79. if (zipFile == null) {
  80. return;
  81. }
  82. if (!zipFile.getName().endsWith(".zip")) {
  83. return;
  84. }
  85. ZipOutputStream zos = null;
  86. FileOutputStream out = new FileOutputStream(zipFile);
  87. try {
  88. zos = new ZipOutputStream(out);
  89. for (File srcFile : srcFiles) {
  90. byte[] buf = new byte[128];
  91. zos.putNextEntry(new ZipEntry(srcFile.getName()));
  92. int len;
  93. // 读取文件并写入到zip中
  94. FileInputStream in = new FileInputStream(srcFile);
  95. while ((len = in.read(buf)) != -1) {
  96. zos.write(buf, 0, len);
  97. zos.flush();
  98. }
  99. in.close();
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. } finally {
  104. if (zos != null) {
  105. zos.close();
  106. }
  107. }
  108. }
  109. }