FileZipUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.xjrsoft.common.utils;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import java.io.*;
  4. import java.nio.charset.Charset;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipFile;
  11. import java.util.zip.ZipOutputStream;
  12. /**
  13. * @author dzx
  14. * @Description 文件压缩工具
  15. * 2023/12/5
  16. */
  17. public class FileZipUtil {
  18. public static ByteArrayInputStream inputStream2ByteArray(InputStream in, boolean isClose) {
  19. byte[] byteArray = null;
  20. try {
  21. int total = in.available();
  22. byteArray = new byte[total];
  23. in.read(byteArray);
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. } finally {
  27. if (isClose) {
  28. try {
  29. in.close();
  30. } catch (Exception e2) {
  31. //zou.log.Logger.getServerlog().error(" ر ʧ ");
  32. }
  33. }
  34. }
  35. return new ByteArrayInputStream(byteArray);
  36. }
  37. public static byte[] byteArray(InputStream in, boolean isClose) {
  38. byte[] byteArray = null;
  39. try {
  40. int total = in.available();
  41. byteArray = new byte[total];
  42. in.read(byteArray);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. } finally {
  46. if (isClose) {
  47. try {
  48. in.close();
  49. } catch (Exception e2) {
  50. //zou.log.Logger.getServerlog().error(" ر ʧ ");
  51. }
  52. }
  53. }
  54. return byteArray;
  55. }
  56. public static void zipFiles(File[] srcfile, File zipfile) {
  57. byte[] buf = new byte[1024];
  58. try {
  59. // ZipOutputStream ࣺ ļ ļ е ѹ
  60. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
  61. for (int i = 0; i < srcfile.length; i++) {
  62. FileInputStream in = new FileInputStream(srcfile[i]);
  63. out.putNextEntry(new ZipEntry(srcfile[i].getName()));
  64. int len;
  65. while ((len = in.read(buf)) > 0) {
  66. out.write(buf, 0, len);
  67. }
  68. out.closeEntry();
  69. in.close();
  70. }
  71. out.close();
  72. } catch (Exception e) {
  73. // TODO Auto-generated catch block
  74. //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
  75. }
  76. }
  77. public static void zipFiles(List<File> fileList, File zipfile) {
  78. byte[] buf = new byte[1024];
  79. try {
  80. // ZipOutputStream ࣺ ļ ļ е ѹ
  81. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
  82. for(File file : fileList) {
  83. FileInputStream in = new FileInputStream(file);
  84. out.putNextEntry(new ZipEntry(file.getName()));
  85. int len;
  86. while ((len = in.read(buf)) > 0) {
  87. out.write(buf, 0, len);
  88. }
  89. out.closeEntry();
  90. in.close();
  91. }
  92. out.close();
  93. } catch (Exception e) {
  94. // TODO Auto-generated catch block
  95. //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
  96. }
  97. }
  98. public static byte[] byteAryMap2Zip(Map<String,byte[]> byteAryMap) {
  99. byte[] buf = new byte[1024];
  100. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  101. try {
  102. // ZipOutputStream ࣺ ļ ļ е ѹ
  103. ZipOutputStream out = new ZipOutputStream(baos);
  104. for(String fileName : byteAryMap.keySet()) {
  105. byte[] byteAry = byteAryMap.get(fileName);
  106. ByteArrayInputStream in = new ByteArrayInputStream(byteAry);
  107. out.putNextEntry(new ZipEntry(fileName));
  108. int len;
  109. while ((len = in.read(buf)) > 0) {
  110. out.write(buf, 0, len);
  111. }
  112. out.closeEntry();
  113. in.close();
  114. }
  115. out.close();
  116. } catch (Exception e) {
  117. // TODO Auto-generated catch block
  118. //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
  119. }
  120. return baos.toByteArray();
  121. }
  122. public static boolean createFloder(File file) {
  123. if (!file.exists()) {
  124. file.mkdir();
  125. }
  126. return true;
  127. }
  128. public static boolean createFile(File file) {
  129. if (!file.exists()) {
  130. try {
  131. file.createNewFile();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. return true;
  137. }
  138. public static String saveFile(String fileName,byte[] fileContent) {
  139. File file1 = new File("D:\\ ļ \\flew\\");
  140. File file = new File("D:\\ ļ \\flew\\"+fileName);
  141. FileOutputStream out = null;
  142. createFloder(file1);
  143. createFile(file);
  144. try {
  145. out = new FileOutputStream(file);
  146. out.write(fileContent);
  147. out.flush();
  148. out.close();
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }finally {
  152. try {
  153. out.close();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. return file.getPath();
  159. }
  160. /**
  161. * 将MultipartFile转成ZipFile
  162. * @param multipartFile
  163. * @return
  164. * @throws IOException
  165. */
  166. public static ZipFile convertToZipFile(MultipartFile multipartFile) throws IOException {
  167. // 创建临时文件
  168. String originalFilename = multipartFile.getOriginalFilename();
  169. Path tempPath = Files.createTempFile("temp", originalFilename.substring(originalFilename.lastIndexOf('.')));
  170. multipartFile.transferTo(tempPath); // 将MultipartFile内容写入临时文件
  171. // 使用ZipFile读取临时文件
  172. ZipFile zipfile = new ZipFile(tempPath.toFile(), Charset.forName("GBK"));
  173. return zipfile;
  174. }
  175. }