Bryant
4 years ago
5 changed files with 64 additions and 133 deletions
@ -1,129 +0,0 @@
|
||||
package com.fr.design.update.factory; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.ArrayUtils; |
||||
import com.fr.stable.StableUtils; |
||||
|
||||
import java.io.BufferedInputStream; |
||||
import java.io.BufferedOutputStream; |
||||
import java.io.DataInputStream; |
||||
import java.io.DataOutputStream; |
||||
import java.io.File; |
||||
import java.io.FileFilter; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* Created by XINZAI on 2018/8/21. |
||||
*/ |
||||
public class DirectoryOperationFactory { |
||||
/** |
||||
* 新建一个目录 |
||||
* |
||||
* @param dirPath 目录路径 |
||||
*/ |
||||
public static void createNewDirectory(String dirPath) { |
||||
try { |
||||
File newDirPath = new File(dirPath); |
||||
if (!newDirPath.exists()) { |
||||
StableUtils.mkdirs(newDirPath); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除目录 |
||||
* |
||||
* @param dirPath 目录路径 |
||||
*/ |
||||
public static void deleteDirectory(String dirPath) { |
||||
try { |
||||
File dir = new File(dirPath); |
||||
if (dir.isDirectory()) { |
||||
File[] file = dir.listFiles(); |
||||
for (File fileTemp : file) { |
||||
deleteDirectory(fileTemp.toString()); |
||||
fileTemp.delete(); |
||||
} |
||||
} else { |
||||
dir.delete(); |
||||
} |
||||
dir.delete(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 复制目录 |
||||
* |
||||
* @param oldDirPath 被复制目录 |
||||
* @param newDirPath 新目录 |
||||
*/ |
||||
public static void copyDirectory(String oldDirPath, String newDirPath) { |
||||
File oldDir = new File(oldDirPath); |
||||
if (oldDir.isDirectory()) { |
||||
StableUtils.mkdirs(new File(newDirPath)); |
||||
File[] files = oldDir.listFiles(); |
||||
for (File fileTemp : files) { |
||||
copyDirectory(fileTemp.toString(), newDirPath + "/" + fileTemp.getName()); |
||||
} |
||||
} else { |
||||
try { |
||||
copy(oldDirPath, newDirPath); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static void copy(String path1, String path2) throws IOException { |
||||
try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path1))); |
||||
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path2)))) { |
||||
byte[] date = new byte[in.available()]; |
||||
in.read(date); |
||||
out.write(date); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 移动目录 |
||||
* |
||||
* @param oldDirPath 被移动目录 |
||||
* @param newDirPath 新目录 |
||||
*/ |
||||
public static void moveDirectory(String oldDirPath, String newDirPath) { |
||||
copyDirectory(oldDirPath, newDirPath); |
||||
deleteDirectory(oldDirPath); |
||||
} |
||||
|
||||
/** |
||||
* 列出过滤后的文件 |
||||
* |
||||
* @param installHome 安装目录 |
||||
* @param backupdir 备份目录 |
||||
* @return String数组 |
||||
*/ |
||||
public static String[] listFilteredFiles(String installHome, String backupdir) { |
||||
File backupDir = new File(StableUtils.pathJoin(installHome, backupdir)); |
||||
StableUtils.mkdirs(backupDir); |
||||
File[] fileNames = backupDir.listFiles(new FileFilter() { |
||||
@Override |
||||
public boolean accept(File pathname) { |
||||
return pathname.isDirectory(); |
||||
} |
||||
}); |
||||
String[] jarFileName = new String[fileNames.length]; |
||||
int j = 0; |
||||
for (File fileName : fileNames) { |
||||
if ((fileName.isDirectory()) && (ArrayUtils.getLength(fileName.listFiles()) > 0)) {//判断备份文件夹中是否为空,为空不显示
|
||||
jarFileName[j++] = fileName.getName(); |
||||
} |
||||
} |
||||
return Arrays.copyOf(jarFileName, j); |
||||
} |
||||
} |
@ -0,0 +1,38 @@
|
||||
package com.fr.design.update.utils; |
||||
|
||||
import com.fr.stable.StableUtils; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author Bryant |
||||
* @version 10.0 |
||||
* Created by Bryant on 2020-09-25 |
||||
*/ |
||||
public class FileUtils { |
||||
|
||||
/** |
||||
* 列出过滤后的文件 |
||||
* |
||||
* @param installHome 安装目录 |
||||
* @param backupdir 备份目录 |
||||
* @return String数组 |
||||
*/ |
||||
public static String[] listFilteredFiles(String installHome, String backupdir) { |
||||
File backupDir = new File(StableUtils.pathJoin(installHome, backupdir)); |
||||
StableUtils.mkdirs(backupDir); |
||||
File[] versionBackup = backupDir.listFiles(); |
||||
List<String> versions = new ArrayList<>(); |
||||
if (versionBackup != null) { |
||||
for (File file : versionBackup) { |
||||
if (file.isDirectory() && file.length() > 0) { |
||||
versions.add(file.getName()); |
||||
} |
||||
} |
||||
} |
||||
String[] result = new String[versions.size()]; |
||||
return versions.toArray(result); |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.fr.design.update.utils; |
||||
|
||||
import com.fr.stable.StableUtils; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
import java.io.File; |
||||
|
||||
/** |
||||
* @author Bryant |
||||
* @version 10.0 |
||||
* Created by Bryant on 2020-09-25 |
||||
*/ |
||||
public class FileUtilsTest { |
||||
|
||||
@Test |
||||
public void testListFilteredFiles() { |
||||
String[] result = FileUtils.listFilteredFiles("home", "backup"); |
||||
Assert.assertEquals(0, result.length); |
||||
StableUtils.deleteFile(new File(StableUtils.pathJoin("home"))); |
||||
} |
||||
} |
Loading…
Reference in new issue