递归获取zip中的文件

 1 //tempPath  系统临时文件夹
 2  2 private List<String> getWsContentList(String tempPath) {
 3  3      //wsFilePath  存放zip文件夹路径
 4  4         File file = new File(wsFilePath);
 5  5         File unzipFile = null;
 6  6      
 7  7      //获取所有zip文件
 8  8         File[] files = file.listFiles();
 9  9         try {
10 10             if (files != null && files.length > 0) {
11 11                 String unzipPath;
12 12                 for (File tempFile : files) {
13 13             //创建存放解压文件的临时文件夹
14 14                     unzipPath = StrUtil.format("{}/unzip/{}", tempPath, String.valueOf(System.currentTimeMillis()));
15 15                     unzipFile = new File(unzipPath);
16 16                     if (!unzipFile.exists()) {
17 17                         unzipFile.mkdir();
18 18                     }
19 19                     ZipUtil.unzip(tempFile, unzipFile);
20 20                 }
21 21             }
22 22         } catch (Exception e) {
23 23             log.error(e.getMessage());
24 24         }
25 25 
26 26      //到临时文件夹读取所有解压出来的文件
27 27         File wsFile = new File(StrUtil.format("{}/unzip", tempPath));
28 28         File[] wsFileList = wsFile.listFiles();
29 29         if (wsFileList != null && wsFileList.length > 0) {
30 30             List<String> wsFilePathList = new ArrayList<>();
31 31        //递归获取所有文件路径(非文件夹)
32 32             recursionReadFile(wsFileList, wsFilePathList);
33 33 
           //获取所有文本文件的内容 34 34 List<String> wsContentList = readTextFile(wsFilePathList); 35 35 return wsContentList; 36 36 } 37 37 return null; 38 38 }
 1     public static List<String> recursionReadFile(File[] fileList, List<String> wsFilePathList) {
 2         for (File file : fileList) {
 3             if (file.isDirectory()) {
 4                 fileList = file.listFiles();
 5                 recursionReadFile(fileList, wsFilePathList);
 6             } else {
 7                 wsFilePathList.add(file.getPath());
 8             }
 9         }
10         return wsFilePathList;
11     }
 1     private static List<String> readTextFile(List<String> wsFilePathList) {
 2         if (CollUtil.isNotEmpty(wsFilePathList)) {
 3             FileReader reader;
 4             List<String> wsContent = new ArrayList<>(wsFilePathList.size());
 5             for (int i = 0; i < wsFilePathList.size(); i++) {
 6                 reader = new FileReader(wsFilePathList.get(i));
 7                 wsContent.add(reader.readString());
 8             }
 9             return wsContent;
10         }
11         return null;
12     }

 

  获取系统临时路径:String tempPath = System.getProperty("java.io.tmpdir");

  最后执行完可以删除临时文件中解压出来的文件:FileUtil.clean(new File(StrUtil.format("{}/unzip", tempPath)));

 

  当中用到了三方hutool工具包。

  个人记录,下次再用到可以直接来搬。

posted @ 2021-05-10 10:49  XQYEAH  阅读(224)  评论(0编辑  收藏  举报
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊