用于需找线上系统与准备发布的系统之间的文件差异,并生成差异文件列表。包括寻找有修改的文件,新增的文件和需要删除的文件。

使用步骤,

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 404646846@qq.com
 * @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 404646846@qq.com
     * @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 404646846@qq.com
     * @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 404646846@qq.com
     * @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 404646846@qq.com
     * @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("获取增量文件结束!");
    }
}