ZipUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.jkcredit.invoice.util;
  2. import java.io.*;
  3. import java.nio.charset.Charset;
  4. import java.util.Enumeration;
  5. import java.util.zip.ZipEntry;
  6. import java.util.zip.ZipFile;
  7. import java.util.zip.ZipOutputStream;
  8. /**
  9. * ZipUtils
  10. *
  11. * @author ZENG.XIAO.YAN
  12. * @version v1.0
  13. * @date 2017年11月19日 下午7:16:08
  14. */
  15. public class ZipUtils {
  16. private static final int BUFFEREDSIZE = 1024;
  17. /**
  18. * 压缩文件
  19. *
  20. * @param zipFileName 保存的压缩包文件路径
  21. * @param filePath 需要压缩的文件夹或者文件路径
  22. * @param isDelete 是否删除源文件
  23. * @throws Exception
  24. */
  25. public static void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {
  26. zip(zipFileName, new File(filePath), isDelete);
  27. }
  28. /**
  29. * 压缩文件
  30. *
  31. * @param zipFileName 保存的压缩包文件路径
  32. * @param inputFile 需要压缩的文件夹或者文件
  33. * @param isDelete 是否删除源文件
  34. * @throws Exception
  35. */
  36. public static void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception {
  37. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
  38. if (!inputFile.exists()) {
  39. throw new FileNotFoundException("在指定路径未找到需要压缩的文件!");
  40. }
  41. zip(out, inputFile, "", isDelete);
  42. out.close();
  43. }
  44. /**
  45. * 递归压缩方法
  46. *
  47. * @param out 压缩包输出流
  48. * @param inputFile 需要压缩的文件
  49. * @param base 压缩的路径
  50. * @param isDelete 是否删除源文件
  51. * @throws Exception
  52. */
  53. private static void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception {
  54. // 如果是目录
  55. if (inputFile.isDirectory()) {
  56. File[] inputFiles = inputFile.listFiles();
  57. out.putNextEntry(new ZipEntry(base + "/"));
  58. base = base.length() == 0 ? "" : base + "/";
  59. for (int i = 0; i < inputFiles.length; i++) {
  60. zip(out, inputFiles[i], base + inputFiles[i].getName(), isDelete);
  61. }
  62. } else { // 如果是文件
  63. if (base.length() > 0) {
  64. out.putNextEntry(new ZipEntry(base));
  65. } else {
  66. out.putNextEntry(new ZipEntry(inputFile.getName()));
  67. }
  68. FileInputStream in = new FileInputStream(inputFile);
  69. try {
  70. int len;
  71. byte[] buff = new byte[BUFFEREDSIZE];
  72. while ((len = in.read(buff)) != -1) {
  73. out.write(buff, 0, len);
  74. }
  75. } catch (IOException e) {
  76. throw e;
  77. } finally {
  78. in.close();
  79. }
  80. }
  81. if (isDelete) {
  82. inputFile.delete();
  83. }
  84. }
  85. /**
  86. * 解压文件
  87. *
  88. * @param zipFilePath:需要解压缩的文件
  89. * @param descDir:解压后的目标目录
  90. */
  91. public static void unZipFiles(String zipFilePath, String descDir) throws IOException {
  92. File destFile = new File(descDir);
  93. if (!destFile.exists()) {
  94. destFile.mkdirs();
  95. }
  96. // 解决zip文件中有中文目录或者中文文件
  97. ZipFile zip = new ZipFile(zipFilePath, Charset.forName("GBK"));
  98. for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
  99. ZipEntry entry = (ZipEntry) entries.nextElement();
  100. InputStream in = zip.getInputStream(entry);
  101. String curEntryName = entry.getName();
  102. // 判断文件名路径是否存在文件夹
  103. int endIndex = curEntryName.lastIndexOf('/');
  104. // 替换
  105. String outPath = (descDir + curEntryName).replaceAll("\\\\", "/");
  106. if (endIndex != -1) {
  107. File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
  108. if (!file.exists()) {
  109. file.mkdirs();
  110. }
  111. }
  112. // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
  113. File outFile = new File(outPath);
  114. if (outFile.isDirectory()) {
  115. continue;
  116. }
  117. OutputStream out = new FileOutputStream(outPath);
  118. byte[] buf1 = new byte[1024];
  119. int len;
  120. while ((len = in.read(buf1)) > 0) {
  121. out.write(buf1, 0, len);
  122. }
  123. in.close();
  124. out.close();
  125. }
  126. zip.close();
  127. }
  128. public static void main(String[] args) {
  129. try {
  130. ZipUtils.unZipFiles("E:\\test\\test.zip", "E:\\test\\out\\");
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. }
  135. }