zhaowawj / learning_doc

0 stars 0 forks source link

FileUtilsEx #1

Closed zhaowawj closed 5 years ago

zhaowawj commented 8 years ago
package com.zmx.test.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;

public class FileUtilsEx
{
    public static void main(String[] args) throws Exception
    {
        String path4 = "/home/zmx/Downloads/uncompressed/commons-io-2.4-src";
        String path5 = "/home/zmx/Downloads/uncompressed/commons-io-2.5-src";

        File file4 = new File(path4);
        File file5 = new File(path5);

        Collection<File> items4 = FileUtils.listFiles(file4, null, true);
        Collection<File> items5 = FileUtils.listFiles(file5, null, true);

        Set<String> itemsPath4 = new HashSet<String>();
        Set<String> itemsPath5 = new HashSet<String>();
        for (File file : items4) {
            itemsPath4.add(file.getPath().substring(path4.length() + 1));
        }
        for (File file : items5) {
            itemsPath5.add(file.getPath().substring(path5.length() + 1));
        }

        Collection<String> deleted = CollectionUtils.subtract(itemsPath4, itemsPath5);
        Collection<String> added = CollectionUtils.subtract(itemsPath5, itemsPath4);
        Collection<String> same = CollectionUtils.intersection(itemsPath4, itemsPath5);

        Collection<String> modified = new ArrayList<String>();
        for (String file : same) {
            File file1 = new File(path4 + "/" + file);
            File file2 = new File(path5 + "/" + file);
            if (!FileUtils.contentEquals(file1, file2)) {
                modified.add(file);
            }
        }

        String patchPath = "/home/zmx/Downloads/uncompressed/patch-tmp-1";
        String patchDesc = "/home/zmx/Downloads/uncompressed/patch-tmp-1/patch.xml";

        // 生成xml文件和zip文件
        PrintWriter out = null;
        out =
            new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(patchDesc)), "UTF-8"));
        out.println("<?xml version=\"1.0\"?>");
        out.println("<patches>");
        for (String delete : deleted) {
            out.format("<patch type=\"DELETE\" path=\"%s\" />\n", delete);
        }

        for (String add : added) {
            out.format("<patch type=\"ADD\" path=\"%s\" />\n", add);

            File srcFile = new File(path5 + "/" + add);
            File dstFile = new File(patchPath + "/" + add);
            FileUtils.copyFile(srcFile, dstFile);
        }
        for (String modify : modified) {
            out.format("<patch type=\"MODIFY\" path=\"%s\" />\n", modify);

            File srcFile = new File(path5 + "/" + modify);
            File dstFile = new File(patchPath + "/" + modify);
            FileUtils.copyFile(srcFile, dstFile);
        }

        out.println("</patches>");
        out.close();

        System.out.println(items4.size());
        System.out.println(items5.size());
    }
}