zoukankan      html  css  js  c++  java
  • 搬砖

    public class DirDiff {
    
        public static String getFileMD5(File file) {
            if (!file.isFile()) {
                return null;
            }
            MessageDigest digest = null;
            byte buffer[] = new byte[1024];
            int len;
            try (FileInputStream in = new FileInputStream(file);){
                digest = MessageDigest.getInstance("MD5");
                while ((len = in.read(buffer, 0, 1024)) != -1) {
                    digest.update(buffer, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return new BigInteger(1, digest.digest()).toString(16);
        }
    
    
        public static Map<String, String> getDirMD5(File file, boolean listChild) {
            if (!file.isDirectory()) {
                return null;
            }
            Map<String, String> map = new HashMap<String, String>();
            String md5;
            File files[] = file.listFiles();
            for (File f : files) {
                if (f.isDirectory() && listChild) {
                    map.putAll(getDirMD5(f, listChild));
                } else {
                    md5 = getFileMD5(f);
                    if (md5 != null) {
                        map.put(f.getPath(), md5);
                    }
                }
            }
            return map;
        }
    
        public static void main(String[] args) {
            String dir1 = "C:\IDOS\dev\automation\verification\today_day_end_files\BIS2_baseinfo";
            String dir2 = "C:\IDOS\dev\automation\verification\yesterday_day_end_files\BIS2_baseinfo";
    
            Map<String, String> map = getDirMD5(new File(dir1), true);
            Map<String, String> map2 = getDirMD5(new File(dir2), true);
            
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String fullPath = it.next();
                String key2 = fullPath.replace(dir1, dir2);
                String md5Val = map.get(fullPath);
                String value2 = map2.remove(key2);
                if (value2 == null) {
                    System.out.println("File is NOT exist! ----->  " + key2);
                } else if (!md5Val.equals(value2)) {
                    System.out.println("File content is different! ==> " + fullPath);
                }
            }
            
            it = map2.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String key2 = key.replace(dir2, dir1);
                System.out.println("File is NOT exist! ----->  " + key2);
            }
        }
    

        public String dirDiff(String orgPath, String otherPath, String... expectedFileList) {
            System.out.println("orgPath :" + orgPath);
            System.out.println("otherPath :" + otherPath);
            List<String> msgs = new ArrayList<>();
            File orgFile = new File(orgPath);
            File otherFile = new File(otherPath);
            if (!orgFile.isDirectory() || !otherFile.isDirectory()) {
                return String.format("Comparison is invalid between %s and %s", orgFile.getName(), otherFile.getName());
            }

            Map<String, String> map = getDirMD5(orgFile, true);
            Map<String, String> map2 = getDirMD5(otherFile, true);

            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String fullPath = it.next();
                String key2 = fullPath.replace(orgPath, otherPath);
                String md5Val = map.get(fullPath);
                String value2 = map2.remove(key2);
                if (value2 == null) {
                    msgs.add("File is NOT exist! ----->  " + key2);
                } else if (!md5Val.equals(value2)) {
                    msgs.add("File content is different! ==> " + fullPath);
                }
            }

            it = map2.keySet().iterator();
            while (it.hasNext()) {
                String fullPath = it.next();
                String key2 = fullPath.replace(otherPath, orgPath);
                System.out.println("File is NOT exist! ----->  " + key2);
                msgs.add("File is NOT exist! ----->  " + key2);
            }

            return StringUtils.join(msgs, " ");
        }

    }
  • 相关阅读:
    Atitit 华为基本法 attilax读后感
    Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感
    Atitit 项目版本管理gitflow 与 Forking的对比与使用
    Atitit 管理的模式扁平化管理 金字塔 直线型管理 垂直管理 水平管理 矩阵式管理 网状式样管理 多头管理 双头管理
    Atitit 乌合之众读后感attilax总结 与读后感结构规范总结
    深入理解 JavaScript 异步系列(4)—— Generator
    深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
    深入理解 JavaScript 异步系列(2)—— jquery的解决方案
    深入理解 JavaScript 异步系列(1)——基础
    使用 github + jekyll 搭建个人博客
  • 原文地址:https://www.cnblogs.com/huanglisong/p/13784646.html
Copyright © 2011-2022 走看看