ZipUtils.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.uas.report.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.Enumeration;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipFile;
  11. import java.util.zip.ZipOutputStream;
  12. import org.apache.log4j.Logger;
  13. import com.alibaba.druid.util.StringUtils;
  14. import com.uas.report.core.exception.ReportException;
  15. /**
  16. * 压缩工具类
  17. *
  18. * @author sunyj
  19. * @since 2016年11月1日 下午4:51:38
  20. */
  21. public class ZipUtils {
  22. private static final Logger logger = Logger.getLogger(ZipUtils.class);
  23. /**
  24. * 压缩文件夹
  25. *
  26. * @param sourceFolderPath
  27. * 将压缩的文件夹
  28. * @param zipFilePath
  29. * 压缩后的压缩包路径
  30. */
  31. public static void zipFolder(String sourceFolderPath, String zipFilePath) {
  32. if (StringUtils.isEmpty(sourceFolderPath) || StringUtils.isEmpty(zipFilePath)) {
  33. return;
  34. }
  35. File folder = new File(sourceFolderPath);
  36. if (!folder.exists() || !folder.isDirectory()) {
  37. throw new ReportException("路径不存在或并非文件夹:" + sourceFolderPath);
  38. }
  39. File[] files = folder.listFiles();
  40. if (files.length < 0) {
  41. throw new ReportException("空文件夹:" + sourceFolderPath);
  42. }
  43. try {
  44. ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath));
  45. putNextEntryFromFolder(zipOutputStream, folder, "");
  46. zipOutputStream.close();
  47. logger.info("Zip finished to... " + zipFilePath);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. /**
  53. * 递归将文件夹下的文件进行压缩
  54. *
  55. * @param zipOutputStream
  56. * 压缩包的输出流
  57. * @param folder
  58. * 需要压缩的文件夹
  59. * @param prefix
  60. * 为保持压缩后的路径层次不变,所记录的当前文件夹的相对层级
  61. */
  62. private static void putNextEntryFromFolder(ZipOutputStream zipOutputStream, File folder, String prefix) {
  63. File[] files = folder.listFiles();
  64. try {
  65. for (File file : files) {
  66. if (file.isDirectory()) {
  67. putNextEntryFromFolder(zipOutputStream, file, prefix + file.getName() + File.separator);
  68. } else {
  69. zipOutputStream.putNextEntry(new ZipEntry(prefix + file.getName()));
  70. InputStream inputStream = new FileInputStream(file);
  71. int b;
  72. while ((b = inputStream.read()) != -1) {
  73. zipOutputStream.write(b);
  74. }
  75. inputStream.close();
  76. }
  77. logger.info("Ziped from... " + file.getPath());
  78. }
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. /**
  84. * 解压缩
  85. *
  86. * @param zipFilePath
  87. * 将解压缩的压缩包路径
  88. * @param zipFilePath
  89. * 解压缩后的文件路径
  90. */
  91. public static void unzip(String zipFilePath, String folderPath) {
  92. if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(folderPath)) {
  93. return;
  94. }
  95. File file = new File(zipFilePath);
  96. if (!file.exists()) {
  97. throw new ReportException("压缩包不存在:" + zipFilePath);
  98. }
  99. File folder = new File(folderPath);
  100. if (!folder.exists()) {
  101. folder.mkdirs();
  102. }
  103. if (!folder.isDirectory()) {
  104. throw new ReportException("并非文件夹:" + folderPath);
  105. }
  106. try {
  107. ZipFile zipFile = new ZipFile(file);
  108. Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
  109. while (zipEntries.hasMoreElements()) {
  110. ZipEntry zipEntry = zipEntries.nextElement();
  111. if (zipEntry.isDirectory()) {
  112. continue;
  113. }
  114. InputStream inputStream = zipFile.getInputStream(zipEntry);
  115. File outFile = new File(folder.getPath() + File.separator + zipEntry.getName());
  116. if (!outFile.getParentFile().exists()) {
  117. outFile.getParentFile().mkdirs();
  118. }
  119. String fileName = folder.getPath() + File.separator + zipEntry.getName();
  120. OutputStream outputStream = new FileOutputStream(fileName);
  121. int b;
  122. while ((b = inputStream.read()) != -1) {
  123. outputStream.write(b);
  124. }
  125. inputStream.close();
  126. outputStream.close();
  127. logger.info("Unziped to... " + fileName);
  128. }
  129. zipFile.close();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. logger.info("Unzip finished from... " + zipFilePath);
  134. }
  135. }