java zip文件的多层级遍历和文件夹下目录的问题

灰太狼 2022-03-31 07:17 244阅读 0赞

java zip文件的多层级遍历和文件夹下目录的问题

  1. package com.hgh.zip;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.util.Enumeration;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. import java.util.UUID;
  15. import java.util.zip.ZipEntry;
  16. import java.util.zip.ZipFile;
  17. /**
  18. * https://www.cnblogs.com/invban/p/7975583.html
  19. * @author he_guanhong
  20. *
  21. */
  22. public class ZIPtest {
  23. /**
  24. * 解压zip
  25. *
  26. * @param zipFile
  27. * @param descDir
  28. * @throws Exception
  29. */
  30. public static void unZipFiles(File zipFile, String descDir) throws Exception {
  31. System.out.println("******************解压开始********************");
  32. File pathFile = new File(descDir);
  33. if (!pathFile.exists()) {
  34. pathFile.mkdirs();
  35. }
  36. ZipFile zip = new ZipFile(zipFile);
  37. for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
  38. ZipEntry entry = (ZipEntry) entries.nextElement();
  39. String zipEntryName = entry.getName();
  40. InputStream in = zip.getInputStream(entry);
  41. String outPath = (descDir + "/" + zipEntryName).replaceAll("\\*", "/");
  42. // 判断路径是否存在,不存在则创建文件路径
  43. File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
  44. if (!file.exists()) {
  45. file.mkdirs();
  46. }
  47. // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
  48. if (new File(outPath).isDirectory()) {
  49. continue;
  50. }
  51. OutputStream out = new FileOutputStream(outPath);
  52. byte[] buf1 = new byte[1024];
  53. int len;
  54. while ((len = in.read(buf1)) > 0) {
  55. out.write(buf1, 0, len);
  56. }
  57. if ((zipEntryName.trim().lastIndexOf("/")) == -1) {
  58. }
  59. in.close();
  60. out.close();
  61. }
  62. System.out.println("******************解压完毕********************");
  63. System.out.println("******************遍历文件夹********************");
  64. String parent_id = "";
  65. // traverse(descDir,parent_id);
  66. }
  67. /**
  68. * 文件夹遍历
  69. * @param path
  70. * @throws Exception
  71. */
  72. public static void traverse(String path,String parent_id) throws Exception {
  73. System.out.println("path---->" + path);
  74. File file = new File(path);
  75. Map<String, Object> map = new HashMap<String, Object>();
  76. if (file.exists()) {
  77. File[] files = file.listFiles();
  78. if (files.length == 0) {
  79. System.out.println("文件夹是空的!");
  80. return;
  81. } else {
  82. String k_id = UUID.randomUUID().toString();
  83. for (File file2 : files) {
  84. if (file2.isDirectory()) {//文件夹
  85. traverse(file2.getAbsolutePath(),parent_id);
  86. parent_id = k_id;
  87. } else if (file2.isFile()){//文件
  88. //
  89. // if (file2.getName().endsWith(".cfg")) {
  90. // System.out.println("文件:" + file2.getAbsolutePath());
  91. // map = readCfg(new FileInputStream(file2));
  92. // System.out.println("-------------"+file2.getAbsolutePath()+"--start-----------");
  93. // map.put("path_url", file2.getAbsolutePath());
  94. // map.put("k_id", k_id);
  95. // map.put("parent_id", parent_id);
  96. // for (String key : map.keySet()) {
  97. // System.out.println(key+"--->"+map.get(key));
  98. // }
  99. // parent_id = k_id;
  100. // System.out.println("-------------"+file2.getAbsolutePath()+"--end-----------");
  101. // }
  102. }
  103. }
  104. }
  105. } else {
  106. System.out.println("文件不存在!");
  107. }
  108. }
  109. /**
  110. * 解析cfg文件
  111. *
  112. * @param cfgFile
  113. * @return
  114. */
  115. // public static Map<String, Object> readCfg(FileInputStream cfgFile) {
  116. // Properties prop = new Properties();
  117. // Map<String, Object> map = new HashMap<String, Object>();
  118. //
  119. // try {
  120. // // 读取cfg属性文件
  121. // InputStream in = new BufferedInputStream(cfgFile);
  122. // prop.load(new InputStreamReader(in, "GBK")); /// 加载属性列表
  123. // Iterator<String> it = prop.stringPropertyNames().iterator();
  124. // while (it.hasNext()) {
  125. // String key = it.next();
  126. // map.put(key, prop.getProperty(key).replaceAll("\"", "").replaceAll(";", ""));
  127. // }
  128. // in.close();
  129. // } catch (Exception e) {
  130. // System.out.println(e);
  131. // }
  132. // return map;
  133. // }
  134. public static void main(String[] args) throws Exception {
  135. try {
  136. File file = new File("D:\\授权书图片.zip");
  137. unZipFiles(file, "D:\\授权书图片");
  138. } catch (Exception e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. }
  143. }

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读