Ver código fonte

feat:模版解析日志打印

chenjianhua 1 semana atrás
pai
commit
4463531737

+ 86 - 32
service-base/src/main/java/com/simuwang/base/common/util/ExcelUtil.java

@@ -222,31 +222,66 @@ public class ExcelUtil {
         return cellValue;
     }
 
-    public static List<String> extractCompressedFiles(String zipFilePath, String destFilePath) {
+    public static List<String> extractCompressedFiles(String filePath, String destFilePath) {
         List<String> filePathList = CollUtil.newArrayList();
-        File destFile = new File(destFilePath);
-        if (!destFile.exists()) {
-            destFile.mkdirs();
-        }
-        logger.info("解压文件:"+zipFilePath+"==解压路径"+destFilePath);
-        try (BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFilePath));
-             ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(fis)) {
-            ArchiveEntry entry;
-            while ((entry = ais.getNextEntry()) != null) {
-                File entryFile = new File(destFilePath, entry.getName());
-                if (entry.isDirectory()) {
-                    entryFile.mkdirs();
-                } else {
-                    try (FileOutputStream fos = new FileOutputStream(entryFile)) {
-                        IOUtils.copy(ais, fos);
-                        filePathList.add(entryFile.getPath());
+        try {
+            if(ExcelUtil.isZip(filePath)){
+                File destFile = new File(destFilePath);
+                if (!destFile.exists()) {
+                    destFile.mkdirs();
+                }
+                logger.info("开始解压zip==================="+filePath);
+                BufferedInputStream fis = new BufferedInputStream(new FileInputStream(filePath));
+                ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(fis);
+                ArchiveEntry entry;
+                while ((entry = ais.getNextEntry()) != null) {
+                    String uuid = UUID.randomUUID().toString().replaceAll("-","");
+                    String entryName = entry.getName();
+                    String fileName = null;
+                    if(entryName.contains(".")){
+                        fileName = uuid + entryName.substring(entryName.lastIndexOf("."),entryName.length());
+                    }else{
+                        fileName = uuid;
+                    }
+                    File entryFile = new File(destFilePath, fileName);
+                    if (entry.isDirectory()) {
+                        entryFile.mkdirs();
+                        logger.info("解压子文件:{}",entryFile.getName());
+                    } else {
+                        try {
+                            FileOutputStream fos = new FileOutputStream(entryFile);
+                            IOUtils.copy(ais, fos);
+                            filePathList.add(entryFile.getPath());
+                            logger.info("解压子文件:{}",entryFile.getPath());
+                            fos.close();
+                        }catch (Exception e){
+                            logger.error(e.getMessage(),e);
+                        }
                     }
                 }
+                ais.close();
+                fis.close();
             }
-        } catch (Exception e) {
-            logger.error(e.getMessage(),e);
+            if(ExcelUtil.isRAR(filePath)){
+                logger.info("开始解压RAR==================="+filePath);
+                String destPath = filePath.replaceAll(".rar", "").replaceAll(".RAR", "");
+                File destFile = new File(destPath);
+                if (!destFile.exists()) {
+                    destFile.mkdirs();
+                }
+                List<String> rarDir = ExcelUtil.extractRar(filePath, destPath);
+                for (String subFile : rarDir) {
+                    if(!subFile.contains(destPath)){
+                        subFile = destPath+"/"+subFile;
+                    }
+                    logger.info("解压之后的文件:"+subFile);
+                }
+                filePathList.addAll(rarDir);
+            }
+        }catch (Exception e) {
+            logger.error(filePath+"======="+e.getMessage(),e);
         }
-
+        logger.info("解压结束================");
         return filePathList;
     }
 
@@ -274,21 +309,40 @@ public class ExcelUtil {
                     fileList.add(f.getAbsolutePath());
                 }
             }
-            } catch (Exception e) {
-                logger.error(e.getMessage(),e);
-                //调用unrar命令解压
-                try{
-                    unrar(inputFilePath,outputDirPath);
-                    File dir  = new File(outputDirPath);
-                    for (File file : Objects.requireNonNull(dir.listFiles())) {
-                        logger.info(file.getAbsolutePath());
-                        fileList.add(file.getAbsolutePath());
-                    }
-                }catch (Exception e1){
-                    logger.error(e.getMessage(),e1);
+        } catch (Exception e) {
+            logger.error(e.getMessage(),e);
+            //调用unrar命令解压
+            try{
+                unrar(inputFilePath,outputDirPath);
+                File dir  = new File(outputDirPath);
+                for (File file : dir.listFiles()) {
+                    logger.info(file.getAbsolutePath());
+                    fileList.addAll(fileList(file));
                 }
+            }catch (Exception e1){
+                logger.error(e.getMessage(),e1);
+            }
+        }
+        return fileList;
+    }
 
+    private static List<String> fileList(File file){
+        List<String> fileList = new ArrayList<>();
+        if(file.isDirectory()){
+            File[] files = file.listFiles();
+            for (File file1 : files) {
+                System.out.println(file1.getAbsolutePath());
+                if(file1.isDirectory()){
+                    List<String> dirFileList =  fileList(file1);
+                    fileList.addAll(dirFileList);
+                }else{
+                    fileList.add(file1.getAbsolutePath());
+                }
             }
+        }else{
+            fileList.add(file.getAbsolutePath());
+            System.out.println(file.getAbsolutePath());
+        }
         return fileList;
     }