diff --git a/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java b/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java deleted file mode 100644 index 48c151488a..0000000000 --- a/designer-base/src/main/java/com/fr/design/update/factory/DirectoryOperationFactory.java +++ /dev/null @@ -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); - } -} diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java index 8ffe8d4a41..c9d90f37a7 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java @@ -4,7 +4,7 @@ import com.fr.decision.update.data.UpdateConstants; import com.fr.design.gui.ibutton.UIButton; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.mainframe.DesignerContext; -import com.fr.design.update.factory.DirectoryOperationFactory; +import com.fr.design.update.utils.FileUtils; import com.fr.design.update.ui.widget.ColorfulCellRender; import com.fr.design.utils.gui.GUICoreUtils; import com.fr.stable.ArrayUtils; @@ -86,7 +86,7 @@ public class RestoreDialog extends JDialog { JPanel jarListPane = new JPanel(); jarListPane.setLayout(new BoxLayout(jarListPane, BoxLayout.Y_AXIS)); - String[] jarBackupFiles = DirectoryOperationFactory.listFilteredFiles(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR); + String[] jarBackupFiles = FileUtils.listFilteredFiles(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR); ArrayUtils.reverse(jarBackupFiles); String[] jarFilesList = ((jarBackupFiles.length < NUMOFCELL_LEAST) ? Arrays.copyOf(jarBackupFiles, NUMOFCELL_LEAST) : jarBackupFiles); diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java index 3992be6a91..efbba679d3 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java @@ -17,7 +17,7 @@ import com.fr.design.layout.TableLayoutHelper; import com.fr.design.mainframe.DesignerContext; import com.fr.design.update.actions.FileProcess; import com.fr.design.update.domain.UpdateInfoCachePropertyManager; -import com.fr.design.update.factory.DirectoryOperationFactory; +import com.fr.design.update.utils.FileUtils; import com.fr.design.update.ui.widget.LoadingLabel; import com.fr.design.update.ui.widget.UpdateActionLabel; import com.fr.design.update.ui.widget.UpdateInfoTable; @@ -320,7 +320,7 @@ public class UpdateMainDialog extends UIDialog { } }); //choose RestoreLabel to show - boolean isNeedRestore = ArrayUtils.isNotEmpty(DirectoryOperationFactory.listFilteredFiles(StableUtils.getInstallHome(), getBackupDirectory())); + boolean isNeedRestore = ArrayUtils.isNotEmpty(FileUtils.listFilteredFiles(StableUtils.getInstallHome(), getBackupDirectory())); jarRestoreLabel = isNeedRestore ? jarRestorePreviousRevision : noJarPreviousRevision; } diff --git a/designer-base/src/main/java/com/fr/design/update/utils/FileUtils.java b/designer-base/src/main/java/com/fr/design/update/utils/FileUtils.java new file mode 100644 index 0000000000..db12d2fcd6 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/update/utils/FileUtils.java @@ -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 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); + } +} diff --git a/designer-base/src/test/java/com/fr/design/update/utils/FileUtilsTest.java b/designer-base/src/test/java/com/fr/design/update/utils/FileUtilsTest.java new file mode 100644 index 0000000000..0560cc0281 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/update/utils/FileUtilsTest.java @@ -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"))); + } +}