瀏覽代碼

打分复制

root 2 年之前
父節點
當前提交
f1593ec20e
共有 1 個文件被更改,包括 20 次插入13 次删除
  1. 20 13
      api-common/src/main/java/api/common/util/FileUtil.java

+ 20 - 13
api-common/src/main/java/api/common/util/FileUtil.java

@@ -39,9 +39,14 @@ public class FileUtil {
     public static void cpR(String sourceDirectoryPath, String targetDirectoryPath) {
         File[] files = new File(sourceDirectoryPath).listFiles();
         for (File file : files) {
-            String sourceFilePath = file.getAbsolutePath();
-            String targetFilePath = sourceFilePath.replace(sourceDirectoryPath, targetDirectoryPath);
-            cp(sourceFilePath, targetFilePath);
+            if (file.isDirectory()) {
+                file.mkdir();
+            }
+            if (file.isFile()) {
+                String sourceFilePath = file.getAbsolutePath();
+                String targetFilePath = sourceFilePath.replace(sourceDirectoryPath, targetDirectoryPath);
+                cp(sourceFilePath, targetFilePath);
+            }
         }
     }
 
@@ -104,24 +109,25 @@ public class FileUtil {
 
     /**
      * 获取当前目录下的一级文件全路径列表
+     *
      * @param path 根路径
      * @return 文件全路径列表
      */
     public static List<String> ls(String path) {
         File[] files = new File(path).listFiles();
-        if (files == null){
+        if (files == null) {
             throw new RuntimeException("路径不存在!");
         }
         List<String> result = new ArrayList<>();
         for (File file : files) {
-            result.add(file.getAbsolutePath()) ;
+            result.add(file.getAbsolutePath());
         }
         return result;
     }
 
 
     @SneakyThrows
-    public static String read(String path){
+    public static String read(String path) {
         return read(getFile(path));
     }
 
@@ -415,7 +421,7 @@ public class FileUtil {
 
 
         }
-        if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)|| ".gz".equals(fileType)) {
+        if (".tar.gz".equals(fileType) || ".tgz".equals(fileType) || ".gz".equals(fileType)) {
             TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
             TarArchiveEntry entry;
             // 将 tar 文件解压到 extractPath 目录下
@@ -443,6 +449,7 @@ public class FileUtil {
 
     /**
      * 扫描压缩包中是否存在指定文件
+     *
      * @param inputStream
      * @param fileType
      * @return
@@ -454,17 +461,17 @@ public class FileUtil {
             TarArchiveEntry entry;
             // 将 tar 文件解压到 extractPath 目录下
             while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
-                if (entry.getName().contains(targetFileName)){
+                if (entry.getName().contains(targetFileName)) {
                     return true;
                 }
             }
         }
-        if (".tar.gz".equals(fileType)|| ".tgz".equals(fileType)|| ".gz".equals(fileType)) {
+        if (".tar.gz".equals(fileType) || ".tgz".equals(fileType) || ".gz".equals(fileType)) {
             TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream((new GzipCompressorInputStream(inputStream)));
             TarArchiveEntry entry;
             // 将 tar 文件解压到 extractPath 目录下
             while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
-                if (entry.getName().contains(targetFileName)){
+                if (entry.getName().contains(targetFileName)) {
                     return true;
                 }
             }
@@ -626,13 +633,13 @@ public class FileUtil {
         return result;
     }
 
-    public static void deleteFileBySubstring(String parentDirectoryPath,String substringOfFilename){
+    public static void deleteFileBySubstring(String parentDirectoryPath, String substringOfFilename) {
         File[] files = new File(parentDirectoryPath).listFiles();
-        if(files == null || files.length == 0){
+        if (files == null || files.length == 0) {
             return;
         }
         for (File file : files) {
-            if (file.getName().contains(substringOfFilename)){
+            if (file.getName().contains(substringOfFilename)) {
                 boolean delete = file.delete();
             }
         }