|
@@ -1,234 +0,0 @@
|
|
|
-package com.persagy.ibms.data.sdk.util;
|
|
|
-
|
|
|
-import java.io.BufferedWriter;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.OutputStreamWriter;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.persagy.ibms.core.util.FastJsonCompareUtil;
|
|
|
-import com.persagy.ibms.core.util.FastJsonReaderUtil;
|
|
|
-
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-public class FileUtil {
|
|
|
- public static void Save(String filename, String content) throws Exception {
|
|
|
- Save(filename, content, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static void Save(String filename, String content, List<JSONObject> downloadList) throws Exception {
|
|
|
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
|
|
|
- writer.write(content);
|
|
|
- writer.close();
|
|
|
- if (downloadList != null) {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
- JSONObject json = new JSONObject();
|
|
|
- json.put("time", sdf.format(new Date()));
|
|
|
- json.put("filename", filename);
|
|
|
- json.put("filesize", content.length());
|
|
|
- downloadList.add(json);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void deleteRecursive(File file) {
|
|
|
- if (!file.exists()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (file.isDirectory()) {
|
|
|
- File[] fs = file.listFiles();
|
|
|
- for (int i = 0; i < fs.length; i++) {
|
|
|
- File f = fs[i];
|
|
|
- if (f.isFile()) {
|
|
|
- f.delete();
|
|
|
- } else if (f.isDirectory()) {
|
|
|
- deleteRecursive(f);
|
|
|
- f.delete();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- file.delete();
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean compareRecursive(File file1, File file2, LogOfDownload LogOfDownload) throws Exception {
|
|
|
- if (file1.isDirectory() && file2.isDirectory()) {
|
|
|
- boolean result = true;
|
|
|
- Map<String, File> fileMap1 = new ConcurrentHashMap<String, File>();
|
|
|
- Map<String, File> dirMap1 = new ConcurrentHashMap<String, File>();
|
|
|
- draw(file1, fileMap1, dirMap1);
|
|
|
- Map<String, File> fileMap2 = new ConcurrentHashMap<String, File>();
|
|
|
- Map<String, File> dirMap2 = new ConcurrentHashMap<String, File>();
|
|
|
- draw(file2, fileMap2, dirMap2);
|
|
|
-
|
|
|
- if (!compareKey(fileMap1, fileMap2) || !compareKey(dirMap1, dirMap2)) {
|
|
|
- result = false;
|
|
|
-
|
|
|
- if (LogOfDownload != null) {
|
|
|
- JSONArray file_old = new JSONArray();
|
|
|
- JSONArray file_new = new JSONArray();
|
|
|
- JSONArray dir_old = new JSONArray();
|
|
|
- JSONArray dir_new = new JSONArray();
|
|
|
- if (!compareKey(fileMap1, fileMap2)) {
|
|
|
- for (String key : fileMap1.keySet()) {
|
|
|
- if (!fileMap2.containsKey(key)) {
|
|
|
- file_old.add(fileMap1.get(key).getPath());
|
|
|
- }
|
|
|
- }
|
|
|
- for (String key : fileMap2.keySet()) {
|
|
|
- if (!fileMap1.containsKey(key)) {
|
|
|
- file_new.add(fileMap2.get(key).getPath());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!compareKey(dirMap1, dirMap2)) {
|
|
|
- for (String key : dirMap1.keySet()) {
|
|
|
- if (!dirMap2.containsKey(key)) {
|
|
|
- dir_old.add(dirMap1.get(key).getPath());
|
|
|
- }
|
|
|
- }
|
|
|
- for (String key : dirMap2.keySet()) {
|
|
|
- if (!dirMap1.containsKey(key)) {
|
|
|
- dir_new.add(dirMap2.get(key).getPath());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- JSONObject file_change = new JSONObject();
|
|
|
- file_change.put("dir_name", file1.getPath());
|
|
|
- if (file_old.size() > 0) {
|
|
|
- file_change.put("file_old", file_old);
|
|
|
- }
|
|
|
- if (file_new.size() > 0) {
|
|
|
- file_change.put("file_new", file_new);
|
|
|
- }
|
|
|
- if (dir_old.size() > 0) {
|
|
|
- file_change.put("dir_old", dir_old);
|
|
|
- }
|
|
|
- if (dir_new.size() > 0) {
|
|
|
- file_change.put("dir_new", dir_new);
|
|
|
- }
|
|
|
- LogOfDownload.changeList.add(file_change);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for (String key : fileMap1.keySet()) {
|
|
|
- if (fileMap2.containsKey(key)) {
|
|
|
- File tmp1 = fileMap1.get(key);
|
|
|
- File tmp2 = fileMap2.get(key);
|
|
|
- if (!compareRecursive(tmp1, tmp2, LogOfDownload)) {
|
|
|
- result = false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- for (String key : dirMap1.keySet()) {
|
|
|
- if (dirMap2.containsKey(key)) {
|
|
|
- File tmp1 = dirMap1.get(key);
|
|
|
- File tmp2 = dirMap2.get(key);
|
|
|
- if (!compareRecursive(tmp1, tmp2, LogOfDownload)) {
|
|
|
- result = false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
- } else if (file1.isFile() && file2.isFile()) {
|
|
|
- Object json1 = FastJsonReaderUtil.Instance().Read(file1);
|
|
|
- Object json2 = FastJsonReaderUtil.Instance().Read(file2);
|
|
|
- boolean file_compare = FastJsonCompareUtil.Instance().CompareObject(json1, json2, true);
|
|
|
- // boolean file_compare = file1.length() == file2.length();
|
|
|
- log.debug("Compare " + file1.getPath() + " " + file2.getPath() + "\t" + file_compare);
|
|
|
- if (!file_compare) {
|
|
|
- if (LogOfDownload != null) {
|
|
|
- JSONObject file_change = new JSONObject();
|
|
|
- file_change.put("file_name", file1.getPath());
|
|
|
- file_change.put("length_old", file1.length());
|
|
|
- file_change.put("length_new", file2.length());
|
|
|
- LogOfDownload.changeList.add(file_change);
|
|
|
- }
|
|
|
- }
|
|
|
- return file_compare;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static boolean compareKey(Map<String, File> Map1, Map<String, File> Map2) {
|
|
|
- if (Map1.size() != Map2.size()) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- for (String key1 : Map1.keySet()) {
|
|
|
- if (!Map2.containsKey(key1)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- private static void draw(File file1, Map<String, File> fileMap1, Map<String, File> dirMap1) {
|
|
|
- File[] fs1 = file1.listFiles();
|
|
|
- for (int i = 0; i < fs1.length; i++) {
|
|
|
- File f = fs1[i];
|
|
|
- if (f.isFile()) {
|
|
|
- if (f.getName().endsWith(".json")) {
|
|
|
- fileMap1.put(f.getName(), f);
|
|
|
- }
|
|
|
- } else if (f.isDirectory()) {
|
|
|
- dirMap1.put(f.getName(), f);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断文件是否存在,如果path为null,则返回false
|
|
|
- *
|
|
|
- * @param path
|
|
|
- * 文件路径
|
|
|
- * @return 如果存在返回true
|
|
|
- */
|
|
|
- public static boolean exist(String path) {
|
|
|
- return (null != path) && (cn.hutool.core.io.FileUtil.file("config/" + path).exists() || cn.hutool.core.io.FileUtil.file(path).exists());
|
|
|
- }
|
|
|
-
|
|
|
- public static File file(String path) {
|
|
|
- if (null == path) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- path = StrUtil.removePrefix(path, File.separator);
|
|
|
- String filePath = System.getProperty("user.dir") + File.separator + "config" + File.separator + path;
|
|
|
- if (cn.hutool.core.io.FileUtil.exist(filePath)) {
|
|
|
- log.info("文件{}获取1路径:{}", path, filePath);
|
|
|
- return cn.hutool.core.io.FileUtil.file(filePath);
|
|
|
- }
|
|
|
- filePath = System.getProperty("user.dir") + File.separator + path;
|
|
|
- if (cn.hutool.core.io.FileUtil.exist(filePath)) {
|
|
|
- log.info("文件{}获取2路径:{}", path, filePath);
|
|
|
- return cn.hutool.core.io.FileUtil.file(filePath);
|
|
|
- }
|
|
|
- if (cn.hutool.core.io.FileUtil.exist("config/" + path)) {
|
|
|
- log.info("文件{}获取3路径:{}", path, cn.hutool.core.io.FileUtil.file("config/" + path).getPath());
|
|
|
- return cn.hutool.core.io.FileUtil.file("config/" + path);
|
|
|
- }
|
|
|
- if (cn.hutool.core.io.FileUtil.exist(path)) {
|
|
|
- log.info("文件{}获取4路径:{}", path, cn.hutool.core.io.FileUtil.file(path).getPath());
|
|
|
- return cn.hutool.core.io.FileUtil.file(path);
|
|
|
- }
|
|
|
- log.info("文件{}获取5路径:{}", path, path);
|
|
|
- return new File(path);
|
|
|
- }
|
|
|
-
|
|
|
- // public static File file(String path) {
|
|
|
- // if (null == path) {
|
|
|
- // return null;
|
|
|
- // }
|
|
|
- // return new File(path);
|
|
|
- // }
|
|
|
-}
|