FolderCompareFrame


点击(此处)折叠或打开

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.LinkedHashSet;
  15. import java.util.List;
  16. import java.util.Properties;
  17. import java.util.Set;

  18. import javax.swing.DefaultListModel;
  19. import javax.swing.JButton;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JList;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JTextField;
  26. import javax.swing.SwingUtilities;


  27. public class FolderCompareFrame2 extends JFrame implements ActionListener {

  28.     private static final String[] SUFFIX_FILTER = new String[] {".java", ".properties", ".xml", ".jsp", ".sql", ".jar", ".php" };
  29.     private static final String[] EXCLUDE_FILTER = new String[] {"/test/", "/integration/", "/target/"};
  30.     
  31.     private static final long serialVersionUID = 3278358999317656288L;
  32.     private static final String notepadPlusCmd = "C:/Program Files (x86)/Notepad++/notepad++.exe";
  33.     
  34.     private JTextField sourceInput;
  35.     private JTextField targetInput;
  36.     private JButton compareButton;
  37.     private JList resultList;
  38.     private DefaultListModel resultListModel = new DefaultListModel();
  39.     
  40.     private String sourceFolder;
  41.     private String targetFolder;
  42.     
  43.     private String inputFolder1 = "";
  44.     private String inputFolder2 = "";

  45.     public FolderCompareFrame2() {
  46.         loadSettings();
  47.         setSize(800, 600);
  48.         
  49.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  50.         setTitle("Folder Compare");
  51.         setLayout(null);
  52.         sourceInput = addSourceFolderInput();
  53.         targetInput = addTargetFolderInput();
  54.         compareButton = addCompareButton();
  55.         resultList = addResultList();
  56.         setVisible(true);
  57.         
  58.         compareButton.addActionListener(this);
  59.         resultList.addMouseListener(new MouseAdapter() {

  60.             @Override
  61.             public void mouseClicked(MouseEvent e) {
  62.                 if (e.getClickCount() == 2) {
  63.                     String file = (String) resultList.getSelectedValue();
  64.                     compareFile(sourceFolder + file, targetFolder + file);
  65.                 }
  66.             }
  67.             
  68.         });
  69.     }
  70.     
  71.     private void loadSettings() {
  72.         Properties props = new Properties();
  73.         try {
  74.             props.load(new FileInputStream(this.getClass().getSimpleName() + ".config"));
  75.             inputFolder1 = props.getProperty("folder1");
  76.             inputFolder2 = props.getProperty("folder2");
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.     
  82.     private void saveSettings() {
  83.         Properties props = new Properties();
  84.         try {
  85.             props.put("folder1", inputFolder1);
  86.             props.put("folder2", inputFolder2);
  87.             props.store(new FileOutputStream(this.getClass().getSimpleName() + ".config"), null);
  88.         } catch (Exception e) {
  89.             e.printStackTrace();
  90.         }
  91.     }

  92.     private JList addResultList() {
  93.         JList list = new JList();
  94.         JScrollPane pane = new JScrollPane(list);
  95.         pane.setBounds(10, 80, 750, 450);
  96.         add(pane);
  97.         return list;
  98.     }

  99.     private JButton addCompareButton() {
  100.         JButton button = new JButton("Compare");
  101.         button.setBounds(620, 15, 100, 50);
  102.         add(button);
  103.         return button;
  104.     }

  105.     private JTextField addTargetFolderInput() {
  106.         JLabel label = new JLabel("Target:");
  107.         label.setBounds(10, 40, 100, 30);
  108.         add(label);
  109.         
  110.         final JTextField input = new JTextField(inputFolder2);
  111.         input.setBounds(70, 40, 500, 30);
  112.         add(input);
  113.         
  114.         JButton button = new JButton("...");
  115.         button.setBounds(570, 40, 30, 30);
  116.         add(button);
  117.         final JFileChooser fileChooser = new JFileChooser();
  118.         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  119.         button.addActionListener(new ActionListener() {
  120.             @Override
  121.             public void actionPerformed(ActionEvent e) {
  122.                 int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
  123.                 if(result == JFileChooser.APPROVE_OPTION) {
  124.                     input.setText(fileChooser.getSelectedFile().getAbsolutePath());
  125.                     inputFolder2 = fileChooser.getSelectedFile().getAbsolutePath();
  126.                 }
  127.             }
  128.         });

  129.         return input;
  130.     }

  131.     private JTextField addSourceFolderInput() {
  132.         JLabel label = new JLabel("Source:");
  133.         label.setBounds(10, 10, 100, 30);
  134.         add(label);
  135.         
  136.         final JTextField input = new JTextField(inputFolder1);
  137.         input.setBounds(70, 10, 500, 30);
  138.         add(input);
  139.         
  140.         JButton button = new JButton("...");
  141.         button.setBounds(570, 10, 30, 30);
  142.         add(button);
  143.         final JFileChooser fileChooser = new JFileChooser();
  144.         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  145.         button.addActionListener(new ActionListener() {
  146.             @Override
  147.             public void actionPerformed(ActionEvent e) {
  148.                 int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
  149.                 if(result == JFileChooser.APPROVE_OPTION) {
  150.                     input.setText(fileChooser.getSelectedFile().getAbsolutePath());
  151.                     inputFolder1 = fileChooser.getSelectedFile().getAbsolutePath();
  152.                 }
  153.             }
  154.         });

  155.         return input;
  156.     }

  157.     public static void main(String[] args) {
  158.         new FolderCompareFrame2();
  159.     }

  160.     @Override
  161.     public void actionPerformed(ActionEvent e) {
  162.         sourceFolder = sourceInput.getText().replaceAll("\\\\", "/");
  163.         if (!sourceFolder.endsWith("/")) {
  164.             sourceFolder = sourceFolder + "/";
  165.         }
  166.         targetFolder = targetInput.getText().replaceAll("\\\\", "/");
  167.         if (!targetFolder.endsWith("/")) {
  168.             targetFolder = targetFolder + "/";
  169.         }
  170.         SwingUtilities.invokeLater(new Runnable() {
  171.             @Override
  172.             public void run() {
  173.                 saveSettings();
  174.                 resultListModel = new DefaultListModel();
  175.                 compare(sourceFolder, targetFolder);
  176.                 resultList.setModel(resultListModel);
  177.             }
  178.         });
  179.         
  180.     }
  181.     
  182.     /**
  183.      * Start NotePad++
  184.      * @param sourceFile
  185.      * @param targetFile
  186.      */
  187.     private void compareFile(String sourceFile, String targetFile) {
  188.         ProcessBuilder pb = new ProcessBuilder(notepadPlusCmd, sourceFile, targetFile);
  189.         try {
  190.             pb.start();
  191.         } catch (Exception e) {
  192.             e.printStackTrace();
  193.         }
  194.     }
  195.     
  196.     private boolean endWith(String file, String[] suffix) {
  197.         for (String s : suffix) {
  198.             if (file.endsWith(s)) {
  199.                 return true;
  200.             }
  201.         }
  202.         return false;
  203.     }
  204.     
  205.     private boolean containString(String file, String[] containString) {
  206.         for (String s : containString) {
  207.             if (file.contains(s)) {
  208.                 return true;
  209.             }
  210.         }
  211.         return false;
  212.     }
  213.     
  214.     private List<String> list(File folder) {
  215.         if (folder.isFile()) {
  216.             return Collections.singletonList(folder.getAbsolutePath().replace("\\", "/"));
  217.         }
  218.         List<String> list = new ArrayList<String>();
  219.         for (File f : folder.listFiles()) {
  220.             list.addAll(list(f));
  221.         }
  222.         return list;
  223.     }

  224.     private void compare(String source, String target) {
  225.         System.out.println("Compare File: " + source + " with " + target);
  226.         File sourceFile = new File(source);
  227.         File targetFile = new File(target);
  228.         
  229.         if (!sourceFile.exists() || !targetFile.exists()) {
  230.             //Source file or target file not exist.
  231.             if ((sourceFile.isFile() || targetFile.isFile())
  232.                     && endWith(source, SUFFIX_FILTER) && !containString(source, EXCLUDE_FILTER)) {
  233.                 resultListModel.addElement(source.substring(sourceFolder.length()));
  234.             }
  235.             
  236.             //Source file is a folder, target folder not exist.
  237.             if (sourceFile.isDirectory()) {
  238.                 for (String f : list(sourceFile)) {
  239.                     if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
  240.                         resultListModel.addElement(f.substring(sourceFolder.length()));
  241.                     }
  242.                 }
  243.             }
  244.             
  245.             if (targetFile.isDirectory()) {
  246.                 for (String f : list(targetFile)) {
  247.                     if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
  248.                         resultListModel.addElement(f.substring(targetFolder.length()));
  249.                     }
  250.                 }
  251.             }
  252.             
  253.         } else if (sourceFile.isFile() && targetFile.isFile() && endWith(source, SUFFIX_FILTER)
  254.                 && !containString(source, EXCLUDE_FILTER) && !fileMatch(sourceFile, targetFile)) {
  255.             resultListModel.addElement(source.substring(sourceFolder.length()));
  256.         } else if (sourceFile.isDirectory() && targetFile.isDirectory()) {
  257.             if (!source.endsWith("/")) {
  258.                 source = source + "/";
  259.             }
  260.             if (!target.endsWith("/")) {
  261.                 target = target + "/";
  262.             }
  263.             
  264.             Set<String> subFolders = new LinkedHashSet<String>();
  265.             subFolders.addAll(Arrays.asList(sourceFile.list()));
  266.             subFolders.addAll(Arrays.asList(targetFile.list()));
  267.             for (String subFolder : subFolders) {
  268.                 compare(source + subFolder, target + subFolder);
  269.             }
  270.         }
  271.     }

  272.     private boolean fileMatch(File sourceFile, File targetFile) {
  273.         if (sourceFile.length() != targetFile.length()) {
  274.             return false;
  275.         }
  276.         
  277.         try {
  278.             if (!Arrays.equals(md5(readFileToByteArray(sourceFile)), md5(readFileToByteArray(targetFile)))) {
  279.                 return false;
  280.             }
  281.         } catch (IOException e) {
  282.             e.printStackTrace();
  283.             return false;
  284.         }
  285.         
  286.         return true;
  287.     }

  288.     private byte[] md5(byte[] buf) {
  289.         try {
  290.             MessageDigest md5 = MessageDigest.getInstance("MD5");
  291.             return md5.digest(buf);
  292.         } catch (NoSuchAlgorithmException e) {
  293.             throw new IllegalArgumentException();
  294.         }
  295.     }

  296.     private byte[] readFileToByteArray(File f) throws IOException {
  297.         FileInputStream input = new FileInputStream(f);
  298.         try {
  299.             if (f.isFile() && f.canRead()) {
  300.                 int size = (int)f.length();
  301.                 byte[] data = new byte[size];
  302.                 
  303.                 int offset = 0;
  304.          int readed;
  305.     
  306.          while (offset < size && (readed = input.read(data, offset, size - offset)) != -1) {
  307.          offset += readed;
  308.          }
  309.                 return data;
  310.             } else {
  311.                 return null;
  312.             }
  313.         } finally {
  314.             input.close();
  315.         }
  316.     }
  317. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10742815/viewspace-2130715/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10742815/viewspace-2130715/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值