Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6.  
  7. public class Sync {
  8.  
  9. private static boolean identical;
  10.  
  11. public static void main(String[] args) {
  12. File S = new File(args[0]), D = new File(args[1]);
  13. identical = true;
  14. sync(S, D, "DELETE ");
  15. sync(D, S, "COPY ");
  16. if (identical) {
  17. System.out.println("IDENTICAL");
  18. }
  19. }
  20.  
  21. private static void sync(File s, File d, String command) {
  22. if (s.exists()) {
  23. ArrayList<String> sf = new ArrayList<>(Arrays.asList(s.list()));
  24. File[] files = d.listFiles();
  25. sf.sort(Comparator.naturalOrder());
  26. Arrays.sort(files, Comparator.comparing(File::getName));
  27. for (File f : files) {
  28. if (f.isDirectory()) {
  29. sync(new File(s, f.getName()), f, command);
  30. } else if (!sf.contains(f.getName())) {
  31. identical = false;
  32. System.out.println(command + f.toString().substring(2));
  33. } else if (new File(s, f.getName()).length() != f.length()) {
  34. try {
  35. File mFile = new File(s, f.getName());
  36. byte[] bf = new byte[(int) f.length()], bd = new byte[(int) mFile.length()];
  37. FileInputStream mFileInputStream = new FileInputStream(f);
  38. mFileInputStream.read(bf);
  39. mFileInputStream.close();
  40. mFileInputStream = new FileInputStream(mFile);
  41. mFileInputStream.read(bd);
  42. mFileInputStream.close();
  43. if (!Arrays.equals(bf, bd)) {
  44. identical = false;
  45. System.out.println(command + f.toString().substring(2));
  46. }
  47. } catch (Throwable t) {
  48. }
  49. }
  50. }
  51. } else {
  52. identical = false;
  53. File[] files = d.listFiles();
  54. Arrays.sort(files, Comparator.comparing(File::getName));
  55. for (File f : files) {
  56. System.out.println(command + f.toString().substring(2));
  57. }
  58. }
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement