点击(此处)折叠或打开
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Properties;
- import java.util.Set;
-
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JScrollPane;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
-
-
- public class FolderCompareFrame2 extends JFrame implements ActionListener {
-
- private static final String[] SUFFIX_FILTER = new String[] {".java", ".properties", ".xml", ".jsp", ".sql", ".jar", ".php" };
- private static final String[] EXCLUDE_FILTER = new String[] {"/test/", "/integration/", "/target/"};
-
- private static final long serialVersionUID = 3278358999317656288L;
- private static final String notepadPlusCmd = "C:/Program Files (x86)/Notepad++/notepad++.exe";
-
- private JTextField sourceInput;
- private JTextField targetInput;
- private JButton compareButton;
- private JList resultList;
- private DefaultListModel resultListModel = new DefaultListModel();
-
- private String sourceFolder;
- private String targetFolder;
-
- private String inputFolder1 = "";
- private String inputFolder2 = "";
-
- public FolderCompareFrame2() {
- loadSettings();
- setSize(800, 600);
-
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setTitle("Folder Compare");
- setLayout(null);
- sourceInput = addSourceFolderInput();
- targetInput = addTargetFolderInput();
- compareButton = addCompareButton();
- resultList = addResultList();
- setVisible(true);
-
- compareButton.addActionListener(this);
- resultList.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getClickCount() == 2) {
- String file = (String) resultList.getSelectedValue();
- compareFile(sourceFolder + file, targetFolder + file);
- }
- }
-
- });
- }
-
- private void loadSettings() {
- Properties props = new Properties();
- try {
- props.load(new FileInputStream(this.getClass().getSimpleName() + ".config"));
- inputFolder1 = props.getProperty("folder1");
- inputFolder2 = props.getProperty("folder2");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- private void saveSettings() {
- Properties props = new Properties();
- try {
- props.put("folder1", inputFolder1);
- props.put("folder2", inputFolder2);
- props.store(new FileOutputStream(this.getClass().getSimpleName() + ".config"), null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- private JList addResultList() {
- JList list = new JList();
- JScrollPane pane = new JScrollPane(list);
- pane.setBounds(10, 80, 750, 450);
- add(pane);
- return list;
- }
-
- private JButton addCompareButton() {
- JButton button = new JButton("Compare");
- button.setBounds(620, 15, 100, 50);
- add(button);
- return button;
- }
-
- private JTextField addTargetFolderInput() {
- JLabel label = new JLabel("Target:");
- label.setBounds(10, 40, 100, 30);
- add(label);
-
- final JTextField input = new JTextField(inputFolder2);
- input.setBounds(70, 40, 500, 30);
- add(input);
-
- JButton button = new JButton("...");
- button.setBounds(570, 40, 30, 30);
- add(button);
- final JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
- if(result == JFileChooser.APPROVE_OPTION) {
- input.setText(fileChooser.getSelectedFile().getAbsolutePath());
- inputFolder2 = fileChooser.getSelectedFile().getAbsolutePath();
- }
- }
- });
-
- return input;
- }
-
- private JTextField addSourceFolderInput() {
- JLabel label = new JLabel("Source:");
- label.setBounds(10, 10, 100, 30);
- add(label);
-
- final JTextField input = new JTextField(inputFolder1);
- input.setBounds(70, 10, 500, 30);
- add(input);
-
- JButton button = new JButton("...");
- button.setBounds(570, 10, 30, 30);
- add(button);
- final JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
- if(result == JFileChooser.APPROVE_OPTION) {
- input.setText(fileChooser.getSelectedFile().getAbsolutePath());
- inputFolder1 = fileChooser.getSelectedFile().getAbsolutePath();
- }
- }
- });
-
- return input;
- }
-
- public static void main(String[] args) {
- new FolderCompareFrame2();
- }
-
- @Override
- public void actionPerformed(ActionEvent e) {
- sourceFolder = sourceInput.getText().replaceAll("\\\\", "/");
- if (!sourceFolder.endsWith("/")) {
- sourceFolder = sourceFolder + "/";
- }
- targetFolder = targetInput.getText().replaceAll("\\\\", "/");
- if (!targetFolder.endsWith("/")) {
- targetFolder = targetFolder + "/";
- }
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- saveSettings();
- resultListModel = new DefaultListModel();
- compare(sourceFolder, targetFolder);
- resultList.setModel(resultListModel);
- }
- });
-
- }
-
- /**
- * Start NotePad++
- * @param sourceFile
- * @param targetFile
- */
- private void compareFile(String sourceFile, String targetFile) {
- ProcessBuilder pb = new ProcessBuilder(notepadPlusCmd, sourceFile, targetFile);
- try {
- pb.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- private boolean endWith(String file, String[] suffix) {
- for (String s : suffix) {
- if (file.endsWith(s)) {
- return true;
- }
- }
- return false;
- }
-
- private boolean containString(String file, String[] containString) {
- for (String s : containString) {
- if (file.contains(s)) {
- return true;
- }
- }
- return false;
- }
-
- private List<String> list(File folder) {
- if (folder.isFile()) {
- return Collections.singletonList(folder.getAbsolutePath().replace("\\", "/"));
- }
- List<String> list = new ArrayList<String>();
- for (File f : folder.listFiles()) {
- list.addAll(list(f));
- }
- return list;
- }
-
- private void compare(String source, String target) {
- System.out.println("Compare File: " + source + " with " + target);
- File sourceFile = new File(source);
- File targetFile = new File(target);
-
- if (!sourceFile.exists() || !targetFile.exists()) {
- //Source file or target file not exist.
- if ((sourceFile.isFile() || targetFile.isFile())
- && endWith(source, SUFFIX_FILTER) && !containString(source, EXCLUDE_FILTER)) {
- resultListModel.addElement(source.substring(sourceFolder.length()));
- }
-
- //Source file is a folder, target folder not exist.
- if (sourceFile.isDirectory()) {
- for (String f : list(sourceFile)) {
- if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
- resultListModel.addElement(f.substring(sourceFolder.length()));
- }
- }
- }
-
- if (targetFile.isDirectory()) {
- for (String f : list(targetFile)) {
- if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
- resultListModel.addElement(f.substring(targetFolder.length()));
- }
- }
- }
-
- } else if (sourceFile.isFile() && targetFile.isFile() && endWith(source, SUFFIX_FILTER)
- && !containString(source, EXCLUDE_FILTER) && !fileMatch(sourceFile, targetFile)) {
- resultListModel.addElement(source.substring(sourceFolder.length()));
- } else if (sourceFile.isDirectory() && targetFile.isDirectory()) {
- if (!source.endsWith("/")) {
- source = source + "/";
- }
- if (!target.endsWith("/")) {
- target = target + "/";
- }
-
- Set<String> subFolders = new LinkedHashSet<String>();
- subFolders.addAll(Arrays.asList(sourceFile.list()));
- subFolders.addAll(Arrays.asList(targetFile.list()));
- for (String subFolder : subFolders) {
- compare(source + subFolder, target + subFolder);
- }
- }
- }
-
- private boolean fileMatch(File sourceFile, File targetFile) {
- if (sourceFile.length() != targetFile.length()) {
- return false;
- }
-
- try {
- if (!Arrays.equals(md5(readFileToByteArray(sourceFile)), md5(readFileToByteArray(targetFile)))) {
- return false;
- }
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
-
- return true;
- }
-
- private byte[] md5(byte[] buf) {
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- return md5.digest(buf);
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalArgumentException();
- }
- }
-
- private byte[] readFileToByteArray(File f) throws IOException {
- FileInputStream input = new FileInputStream(f);
- try {
- if (f.isFile() && f.canRead()) {
- int size = (int)f.length();
- byte[] data = new byte[size];
-
- int offset = 0;
- int readed;
-
- while (offset < size && (readed = input.read(data, offset, size - offset)) != -1) {
- offset += readed;
- }
- return data;
- } else {
- return null;
- }
- } finally {
- input.close();
- }
- }
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10742815/viewspace-2130715/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10742815/viewspace-2130715/