-0x00 Preface
Prior to work, there are often requests for customers to submit bugs or add a feature. So I'll modify the code in my local area and then package the replacement item for the customer. The trouble is, every update needs to go to SVN to see what files have changed, what new files, one to go to find, and then packaging, to customer updates, and sometimes accidentally missed or mistaken, very annoying. In fact, I always want to do is, write a program, let the program to achieve the comparison between the old and new two projects, and then the new project more out of the file or changed the file copied out, so that I can save a lot of work, mainly do not have to compare the artificial one by one. It's too annoying.
-0x01 Ideas
1. First, I need to compare two items. So what do I need to do to package the updated files to the customer? Here are two things:
① the new file.
② the modified file.
③ the deleted file.
2. The next step must be to use the Commons-io package, it is necessary to determine whether a new folder in the old folder in the existence of a file, if not exist, it is new, if there is a need to compare two files is the same, here we use the MD5, Compares two files by calculating the MD5 value of two files.
-0x02 Code
The MD5 algorithm class , which references David's code http://blog.csdn.net/zdwzzu2006/article/details/8064960
PackageNet.wechance.incrementalupdatetool;ImportOrg.apache.commons.codec.binary.Hex;ImportOrg.apache.commons.codec.digest.DigestUtils;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.security.MessageDigest;ImportJava.security.NoSuchAlgorithmException;/** * Calculate large files MD5 * David 2012-10-12 * * Public class CheckMD5 { StaticMessageDigest MD5 =NULL;Static{Try{MD5 = Messagedigest.getinstance ("MD5"); }Catch(NoSuchAlgorithmException ne) {Ne.printstacktrace (); } }/** * Get MD5 value for a file * @return MD5 string * * Public StaticStringgetMD5(File file) {FileInputStream FileInputStream =NULL;Try{FileInputStream =NewFileInputStream (file);byte[] buffer =New byte[8192];intLength while(length = fileinputstream.read (buffer))! =-1) {md5.update (buffer,0, length); }return NewString (Hex.encodehex (Md5.digest ())); }Catch(FileNotFoundException e) {E.printstacktrace ();return NULL; }Catch(IOException e) {E.printstacktrace ();return NULL; }finally{Try{if(FileInputStream! =NULL) Fileinputstream.close (); }Catch(IOException e) {E.printstacktrace (); } } }/** * MD5 value of a string * @param Target String * @return MD5 value * * Public StaticStringMD5(String target) {returnDigestutils.md5hex (target); }}
Main Method Class
Package Net. Wechance. Incrementalupdatetool;import org. Apache. Commons. IO. FileUtils;import org. Apache. Commons. Lang3. StringUtils;Import Java. IO. File;Import Java. IO. IOException;Import Java. IO. InputStream;Import Java. Util. ArrayList;Import Java. Util. Iterator;Import Java. Util. List;Import Java. Util. Properties;/** * Main method * Created by William on 2015/7/10. * *public class Main {private static String OldPath =""; Old Pathprivate static String NewPath =""; New Pathprivate static String Outpath =""; Output Pathstatic {Properties prop = new Properties ();InputStreaminch= Object. Class. getResourceAsStream("/config.properties");try {prop. Load(inch);OldPath = Prop. GetProperty("OldPath"). Trim();NewPath = Prop. GetProperty("NewPath"). Trim();Outpath = Prop. GetProperty("Outpath"). Trim();} catch (IOException e) {E. Printstacktrace();}} private static void Checkfile () {} public static void Main (string[] args) {System. out. println("---------------------------------------");System. out. println("===> program is started");System. out. println("===> is traversing the folder"+ NewPath);Traverse the new file directory Iterator Iterator = FileUtils. Iteratefiles(New File (NewPath), NULL, True);Traverse old file directory Iterator Iterator1 = FileUtils. Iteratefiles(New File (OldPath), NULL, True);Used to receive deleted directories list<string> deletefiles = new Arraylist<string> ();System. out. println("===> traversal complete, start performing analysis");Long StartTime = System. Currenttimemillis(); Get Start timetry {//Traverse compare new/Old directory//1.If the file does not exist, it is a new file, copy the file to the output path//2.If the MD5 value is not the same, the file is modified to copy the file to the output path while (iterator. Hasnext()) {//New file path String npathstr = iterator. Next(). toString();File NewFile = new file (NPATHSTR);Old file path oldFile = new files (npathstr. Replace(NewPath, OldPath));System. out. println("===> is analyzing"+ npathstr);Determine if the file exists if (!oldfile. Exists()) {File OutFile = new File (npathstr. Replace(NewPath, Outpath));FileUtils. CopyFile(NewFile, OutFile);System. out. println("===> has found new files");System. out. println("======> Copy Files"+ OutFile. toString());System. out. println("done.");System. out. println();} else {//MD5 checksum//If not the same, copy to output path String newMD5 = CheckMD5. GETMD5 (NewFile);String oldMD5 = CheckMD5. GETMD5 (OldFile);if (! StringUtils. Equals(NewMD5, oldMD5)) {File OutFile = new File (npathstr. Replace(NewPath, Outpath));FileUtils. CopyFile(NewFile, OutFile);System. out. println("===> has found a modified file");System. out. println("======> new File"+ NewFile +"(MD5:"+ newMD5 +" )");System. out. println("======> old Files"+ OldFile +"(MD5:"+ oldMD5 +" )");System. out. println("======> Copy Files"+ OutFile. toString());System. out. println("done.");System. out. println();}}}//traverse old file directory//main is used to find deleted files System. out. println("===> deleted files found");while (Iterator1. Hasnext()) {//old file path String opathstr = Iterator1. Next(). toString();New file path NewFile = new files (opathstr. Replace(OldPath, NewPath));if (!newfile. Exists()) {Deletefiles. Add(OPATHSTR);System. out. println("======> file path"+ opathstr);}}} catch (Exception e) {System. Err. println("An exception occurred!");} Long Endtime=system. Currenttimemillis();//Get End timeSystem. out. println();System. out. println("Time To complete analysis:"+ ((endtime-starttime)/ +) +"S");System. out. println("Output path:"+ Outpath);System. out. println("---------------------------------------");}}
Config.properties
#旧路径oldPath=F:\\test\\oldPath#新路径newPath=F:\\test\\newPath#输出路径outPath=F:\\test\\outPath
-0x03 Summary
Finally, the project is tested.
Old folder
New Folder
Output folder
Control Desk
All right. I'll be finished by this. Make it work, consider making it a GUI. You can select the two-comparison directory and the output directory with the mouse point.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Compare file additions and modifications under two folders using Java implementation and copy to new directory (implement incremental update project)