用于需找線上系統與準備釋出的系統之間的檔案差異,并生成差異檔案清單。包括尋找有修改的檔案,新增的檔案和需要删除的檔案。
使用步驟,
1 eclipse導入maven工廠;
2 打開IncrementalUpdatetools.java
3 更改類裡面的常量LASTEST_FILE_PATH,改為你自己的指定的增量檔案存放的目錄,必須保證該目錄是空的。
4 右鍵run as java application
5 在控制台輸入需要比較的兩個工程的項目路徑,其中第一個輸入的是正線上上運作的項目目錄,第二個輸入的是準備釋出的項目目錄
已知問題,
1 增量檔案和檔案夾的修改時間和建立時間會發生變動。(fixed)
備注,
未經詳細測試(在windows檔案系統進行過簡單測試,linux未進行測試)
有問題,請回報。謝謝
下載下傳位址
http://down.51cto.com/data/1096372
package com.chase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
/**
*
* @author [email protected]
* @Version : V<1.0> <Mar 01, 2014>
*/
public class IncrementalUpdateTools {
//增量後的檔案存放位址,運作本程式前,請保證改目錄為空目錄
public static final String LASTEST_FILE_PATH = "/difference";
public static String originalPath;
public static String destinationPath;
public final static String WAR_FILE_NAME = "web";
//不參與對比的檔案和檔案夾
public final static String[] skippedFiles = { "([.]svn)","([.]bak)","([.]svn-base)", "(Thumbs[.]db)" };
/**
* 擷取從控制台輸入的目錄路徑
*
* @author [email protected]
* @param name
* 提示
* @return 傳回目錄
*/
public static File path2File(String name) {
System.out.println("請輸入" + name + "的目錄:");
byte[] b = new byte[1024];
int n;
File f = null;
String pathStr;
try {
while (null == f || !f.exists()) {
n = System.in.read(b);
pathStr = new String(b, 0, n - 2);
f = new File(pathStr);
if (f.exists()) {
break;
} else {
System.out.println(name + "的目錄不存在!請重新輸入:");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return f;
}
public static boolean regexMatch(String regex, String name) {
Pattern p;
p = Pattern.compile(regex);
return p.matcher(name).find();
}
/**
* 擷取檔案MD5值
*
* @author [email protected]
* @param ff
* 目錄
* @param pathStr
* 目錄的路徑
* @return
*/
public static Map<String, String> allFiles(File ff, String pathStr) {
Map<String, String> map = new HashMap<String, String>();
File[] files = ff.listFiles();
String filePath, fileMD5;
StringBuffer sb = new StringBuffer();
boolean flag = false;;
FileInputStream fis;
try {
for (File f : files) {
for (String s : skippedFiles) {
if (regexMatch(s, f.getName())) {
sb.append(s + " match ----> "
+ f.getName() + " \n");
flag = true;
break;
}
}
if (flag){
flag = false;
continue;
}
if (f.isFile()) {
filePath = f.getAbsolutePath();
fis = new FileInputStream(f);
fileMD5 = org.apache.commons.codec.digest.DigestUtils
.md5Hex(fis);
System.out.println(filePath);
map.put(filePath.replace(pathStr, ""), fileMD5);
} else {
map.putAll(allFiles(f, pathStr));
}
}
if(null != sb && !"".equals(sb.toString())){
System.out.println("跳過了檔案,");
System.out.println(sb.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
/**
* 對比兩個map,傳回是否存在新增,修改或者删除的檔案
*
* @author [email protected]
* @param oMap
* 源目錄
* @param dMap
* 修改後的目錄
* @return
*/
public static List<List<String>> mapComparison(Map<String, String> oMap,
Map<String, String> dMap) {
Entry<String, String> dEntry;
List<String> amendedList = new ArrayList<String>(), diffList = new ArrayList<String>();
List<List<String>> list = new ArrayList<List<String>>();
int counter = 0;
Iterator<Entry<String, String>> it = dMap.entrySet().iterator();
while (it.hasNext()) {
dEntry = it.next();
if (oMap.containsKey(dEntry.getKey())) {// 如果存在這個檔案
if (oMap.get(dEntry.getKey()).equals(dEntry.getValue())) {// 如果檔案相同
} else {
amendedList.add(dEntry.getKey());
}
} else {// 檔案不存在
diffList.add(dEntry.getKey());
}
counter++;
}
list.add(amendedList);
list.add(diffList);
return list;
}
/**
* 複制檔案
*
* @author [email protected]
* @param srcFile
* 源檔案
* @param destFile
* 目标位址
*/
public static boolean copyFile(File srcFile, File destFile) {
boolean f = false;
try {
// System.out.println("準備将 " + srcFile.getAbsolutePath() + " 移動至 "
// + destFile.getAbsolutePath() + " ");
if (destFile.exists()) {
throw new Exception("檔案已經存在!檔案移動失敗!");
}
if (!destFile.getParentFile().exists()) {
// System.out.println("正在建立:" + destFile.getParent());
if (!destFile.getParentFile().mkdirs()) {
throw new Exception("建立檔案夾失敗!");
} else {
// System.out.println("成功建立檔案夾!");
}
}
destFile.setLastModified(srcFile.lastModified());
// FileInputStream fis = new FileInputStream(srcFile);
// FileOutputStream fos = new FileOutputStream(distFile);
// FileChannel fic = fis.getChannel();
// FileChannel foc = fos.getChannel();
// foc.transferFrom(fic, 0, fic.size());
// fic.close();
// foc.close();
// fis.close();
// fos.close();
FileUtils.copyFile(srcFile, destFile, true);
f = true;
// System.out.println("成功将 " + srcFile.getAbsolutePath() + " 移動至 "
// + destFile.getAbsolutePath() + " ");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
public static void main(String[] args) {
String originalPathStr, destPathStr;
Map<String, String> oMap, dMap;
List<List<String>> list;
List<String> amendList, diffList;
File ff = path2File("現有系統的檔案");
originalPathStr = ff.getAbsolutePath();
oMap = allFiles(ff, originalPathStr);
ff = path2File("修改後的系統檔案");
destPathStr = ff.getAbsolutePath();
dMap = allFiles(ff, destPathStr);
System.out.println("開始尋找有修改和新增的檔案!");
list = mapComparison(oMap, dMap);
System.out.println("結束尋找有修改和新增的檔案!");
amendList = list.get(0);// 有改動的檔案
diffList = list.get(1);// 新增的檔案
System.out.println("開始複制差異檔案!");
for (String amendFile : amendList) {
copyFile(new File(destPathStr + File.separator + amendFile),
new File(LASTEST_FILE_PATH + File.separator + amendFile));
}
for (String diffFile : diffList) {
copyFile(new File(destPathStr + File.separator + diffFile),
new File(LASTEST_FILE_PATH + File.separator + diffFile));
}
System.out.println("差異檔案複制結束!");
System.out.println("開始尋找需要删除的檔案!");
list = mapComparison(dMap, oMap);
System.out.println("結束尋找需要删除的檔案!");
amendList = list.get(0);// 有改動的檔案
System.out.println("有" + amendList.size() + "個檔案需要更新:");
for (String diffFile : amendList) {
System.out.println(originalPathStr + File.separator + diffFile);
}
diffList = list.get(1);// 删除的檔案
System.out.println("有" + diffList.size() + "個檔案需要從原始目錄删除:");
for (String diffFile : diffList) {
System.out.println(originalPathStr + File.separator + diffFile);
}
System.out.println("擷取增量檔案結束!");
}
}