AdvancedZipUtils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.kingdee.shr.cmpdesign.web.handler.utils;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. import java.util.zip.ZipEntry;
  6. import java.util.zip.ZipOutputStream;
  7. /**
  8. * @Title: AdvancedZipUtils
  9. * @Author ÇàÎà
  10. * @Package com.kingdee.shr.cmpdesign.web.handler.utils
  11. * @Date 2026/1/15 18:24
  12. * @description:
  13. */
  14. public class AdvancedZipUtils {
  15. public static void createZipWithCompressionLevel(byte[] fileData, String fileName, String zipFilePath, int compressionLevel) {
  16. try (FileOutputStream fos = new FileOutputStream(zipFilePath)) {
  17. ZipOutputStream zos = new ZipOutputStream(fos);
  18. Throwable var7 = null;
  19. try {
  20. zos.setLevel(compressionLevel);
  21. zos.setComment("Created by Java Zip Utils");
  22. ZipEntry entry = new ZipEntry(fileName);
  23. zos.putNextEntry(entry);
  24. zos.write(fileData);
  25. zos.closeEntry();
  26. } catch (Throwable var32) {
  27. var7 = var32;
  28. throw var32;
  29. } finally {
  30. if (zos != null) {
  31. if (var7 != null) {
  32. try {
  33. zos.close();
  34. } catch (Throwable var31) {
  35. var7.addSuppressed(var31);
  36. }
  37. } else {
  38. zos.close();
  39. }
  40. }
  41. }
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. public static void createZipWithDirectory(byte[] fileData, String filePathInZip, String zipFilePath) {
  47. try (FileOutputStream fos = new FileOutputStream(zipFilePath)) {
  48. ZipOutputStream zos = new ZipOutputStream(fos);
  49. Throwable var6 = null;
  50. try {
  51. ZipEntry entry = new ZipEntry(filePathInZip);
  52. zos.putNextEntry(entry);
  53. zos.write(fileData);
  54. zos.closeEntry();
  55. } catch (Throwable var31) {
  56. var6 = var31;
  57. throw var31;
  58. } finally {
  59. if (zos != null) {
  60. if (var6 != null) {
  61. try {
  62. zos.close();
  63. } catch (Throwable var30) {
  64. var6.addSuppressed(var30);
  65. }
  66. } else {
  67. zos.close();
  68. }
  69. }
  70. }
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. public static void createZipFromMultipleFiles(Map<String, byte[]> files, String zipFilePath) {
  76. try (FileOutputStream fos = new FileOutputStream(zipFilePath)) {
  77. ZipOutputStream zos = new ZipOutputStream(fos);
  78. Throwable var5 = null;
  79. try {
  80. for(Map.Entry<String, byte[]> file : files.entrySet()) {
  81. ZipEntry entry = new ZipEntry((String)file.getKey());
  82. zos.putNextEntry(entry);
  83. zos.write((byte[])file.getValue());
  84. zos.closeEntry();
  85. }
  86. } catch (Throwable var32) {
  87. var5 = var32;
  88. throw var32;
  89. } finally {
  90. if (zos != null) {
  91. if (var5 != null) {
  92. try {
  93. zos.close();
  94. } catch (Throwable var31) {
  95. var5.addSuppressed(var31);
  96. }
  97. } else {
  98. zos.close();
  99. }
  100. }
  101. }
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. }