public classDirDiff {public staticString getFileMD5(File file) {if (!file.isFile()) {return null;
}
MessageDigest digest= null;byte buffer[] = new byte[1024];intlen;try (FileInputStream in = newFileInputStream(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 getDirMD5(File file, booleanlistChild) {if (!file.isDirectory()) {return null;
}
Map map = new HashMap();
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);
}
}
}returnmap;
}public static voidmain(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 map = getDirMD5(new File(dir1), true);
Map map2 = getDirMD5(new File(dir2), true);
Iterator 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 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 map = getDirMD5(orgFile, true);
Map map2 = getDirMD5(otherFile, true);
Iterator 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, "\n");
}
}