Import Java.io.File; Import Java.io.FileInputStream; Import Java.math.BigInteger; Import Java.security.MessageDigest; Import Java.util.HashMap; Import Java.util.Iterator; Import Java.util.Map; ? public class Dirdiff { ???? /** ???? * Get the MD5 value of a single file! ???? * ???? * @param file ???? * @return ???? */ ???? public static String getFileMD5 (file file) { ???????? if (!file.isfile ()) { ???????????? return null; ????????} ???????? MessageDigest digest = null; ???????? FileInputStream in = null; ???????? byte buffer[] = new byte[1024]; ???????? int Len; ???????? try { ???????????? Digest = messagedigest.getinstance ("MD5"); ???????????? in = new FileInputStream (file); ???????????? while (len = in.read (buffer, 0, 1024))! =-1) { ???????????????? Digest.update (buffer, 0, Len); ????????????} ???????????? In.close (); ????????} catch (Exception e) { ???????????? E.printstacktrace (); ???????????? return null; ????????} ???????? BigInteger bigInt = new BigInteger (1, Digest.digest ()); ???????? return bigint.tostring (16); ????} ? ???? /** ???? * Gets the MD5 value of the file in the folder ???? * ???? * @param file ???? * @param listchild ???? *; True recursively files in sub-directories ???? * @return ???? */ ???? 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 (int i = 0; i < files.length; i++) { ???????????? File f = files[i]; ???????????? 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 = "D:\\temp\\server1"; ???????? String Dir2 = "D:\\temp\\server2"; ???????? ???????? 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 key = It.next (); ???????????? String Key2 = Key.replace (Dir1, DIR2); ???????????? String value = Map.get (key); ???????????? String value2 = Map2.remove (Key2); ???????????? if (value2 = = null) { ???????????????? SYSTEM.OUT.PRINTLN (key + "+" + value + "| | File does not exist "); ????????????} else if (!value.equals (value2)) { ???????????????? SYSTEM.OUT.PRINTLN (key + "+" + value + "| |" + value2); ????????????} ????????} ???????? ???????? it = Map2.keyset (). iterator (); ???????? while (It.hasnext ()) { ???????????? String key = It.next (); ???????????? SYSTEM.OUT.PRINTLN ("file does not exist | |" + key + "+" + map2.get (key)); ????????} ???????? ????} ? } |