3aef18b7f800c2deb7db786dccbc88c6855c3ee2.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package org.jeecg.modules.demo.onemap.utils;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.Enumeration;
  8. import com.github.junrar.Archive;
  9. import com.github.junrar.rarfile.FileHeader;
  10. import org.apache.tools.zip.ZipEntry;
  11. import org.apache.tools.zip.ZipFile;
  12. /**
  13. * @Description: TODO
  14. * @author: qiudongbao
  15. * @date: 2021年08月03日 13:46
  16. */
  17. /**
  18. * zip和rar解压缩工具类
  19. *
  20. * @author lenovo
  21. */
  22. public class ZipAndRarTools {
  23. /**
  24. * 解压rar
  25. *
  26. * @param sourceRarPath 需要解压的rar文件全路径
  27. * @param destDirPath 需要解压到的文件目录
  28. * @throws Exception
  29. */
  30. public static void unrar(String sourceRarPath, String destDirPath) throws Exception {
  31. File sourceRar = new File(sourceRarPath);
  32. File destDir = new File(destDirPath);
  33. Archive archive = null;
  34. FileOutputStream fos = null;
  35. System.out.println("Starting 开始解压...");
  36. try {
  37. archive = new Archive(sourceRar);
  38. FileHeader fh = archive.nextFileHeader();
  39. int count = 0;
  40. File destFileName = null;
  41. while (fh != null) {
  42. System.out.println((++count) + ") " + fh.getFileNameString());
  43. String compressFileName = fh.getFileNameString().trim();
  44. destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
  45. if (fh.isDirectory()) {
  46. if (!destFileName.exists()) {
  47. destFileName.mkdirs();
  48. }
  49. fh = archive.nextFileHeader();
  50. continue;
  51. }
  52. if (!destFileName.getParentFile().exists()) {
  53. destFileName.getParentFile().mkdirs();
  54. }
  55. fos = new FileOutputStream(destFileName);
  56. archive.extractFile(fh, fos);
  57. fos.close();
  58. fos = null;
  59. fh = archive.nextFileHeader();
  60. }
  61. archive.close();
  62. archive = null;
  63. System.out.println("Finished 解压完成!");
  64. } catch (Exception e) {
  65. throw e;
  66. } finally {
  67. if (fos != null) {
  68. try {
  69. fos.close();
  70. fos = null;
  71. } catch (Exception e) {
  72. }
  73. }
  74. if (archive != null) {
  75. try {
  76. archive.close();
  77. archive = null;
  78. } catch (Exception e) {
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * 解压Zip文件
  85. *
  86. * @param zipFileName 需要解压缩的文件位置
  87. * @param descFileName 将文件解压到某个路径
  88. * @throws IOException
  89. */
  90. public static void unZip(String zipFileName, String descFileName) throws IOException {
  91. System.out.println("文件解压开始...");
  92. System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); //防止文件名中有中文时出错
  93. System.out.println(System.getProperty("sun.zip.encoding")); //ZIP编码方式
  94. System.out.println(System.getProperty("sun.jnu.encoding")); //当前文件编码方式
  95. System.out.println(System.getProperty("file.encoding")); //这个是当前文件内容编码方式
  96. String descFileNames = descFileName;
  97. if (!descFileNames.endsWith(File.separator)) {
  98. descFileNames = descFileNames + File.separator;
  99. }
  100. try {
  101. ZipFile zipFile = new ZipFile(zipFileName, "UTF-8");
  102. ZipEntry entry = null;
  103. String entryName = null;
  104. String descFileDir = null;
  105. byte[] buf = new byte[4096];
  106. int readByte = 0;
  107. @SuppressWarnings("rawtypes")
  108. Enumeration enums = zipFile.getEntries();
  109. while (enums.hasMoreElements()) {
  110. entry = (ZipEntry) enums.nextElement();
  111. entryName = entry.getName();
  112. descFileDir = descFileNames + entryName;
  113. if (entry.isDirectory()) {
  114. new File(descFileDir).mkdir();
  115. continue;
  116. } else {
  117. new File(descFileDir).getParentFile().mkdir();
  118. }
  119. File file = new File(descFileDir);
  120. OutputStream os = new FileOutputStream(file);
  121. InputStream is = zipFile.getInputStream(entry);
  122. while ((readByte = is.read(buf)) != -1) {
  123. os.write(buf, 0, readByte);
  124. }
  125. os.close();
  126. is.close();
  127. }
  128. zipFile.close();
  129. System.out.println("文件解压成功!");
  130. } catch (Exception e) {
  131. System.out.println("文件解压失败!");
  132. e.printStackTrace();
  133. }
  134. }
  135. public static void main(String[] args) throws Exception {
  136. //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\"));
  137. ZipAndRarTools.unZip("E:\\upFiles\\工程_1627962978919.zip", "e:\\upFiles\\工程_1627962978919");
  138. // ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest");
  139. }
  140. }