Advertisement
And1

Untitled

Mar 30th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Sync {
  5.     static boolean flag = true;
  6.     static final String[] cmd = new String[]{"DELETE ", "COPY "};
  7.  
  8.     public static void main(String[] args) {
  9.         File first = new File(args[0]), second = new File(args[1]);
  10.         delete(first, second, args);
  11.         copy(second, first, args);
  12.         if (flag) {
  13.             System.out.println("IDENTICAL");
  14.         }
  15.     }
  16.  
  17.     public static void delete(File first, File second, String[] args){
  18.         if (first.exists()) {
  19.             ArrayList<String> list = new ArrayList<>(Arrays.asList(first.list()));
  20.             File[] arr = second.listFiles();
  21.             list.sort(Comparator.naturalOrder());
  22.             Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
  23.             for (File f : arr) {
  24.                 if (f.isDirectory()) {
  25.                     delete(new File(first, f.getName()), f, args);
  26.                 } else if (!list.contains(f.getName())) {
  27.                     flag = false;
  28.                     System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
  29.                 } else {
  30.                     try {
  31.                         File file = new File(first, f.getName());
  32.                         byte[] bf = new byte[(int) f.length()], bd = new byte[(int) file.length()];
  33.                         FileInputStream stream = new FileInputStream(f);
  34.                         stream.read(bf);
  35.                         stream.close();
  36.                         stream = new FileInputStream(file);
  37.                         stream.read(bd);
  38.                         stream.close();
  39.                         if (!Arrays.equals(bf, bd)) {
  40.                             flag = false;
  41.                             System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
  42.                         }
  43.                     } catch (IOException e) {
  44.                     }
  45.                 }
  46.             }
  47.         } else {
  48.             flag = false;
  49.             File[] arr = second.listFiles();
  50.             Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
  51.             for (File f : arr) {
  52.                 System.out.println("DELETE " + f.toString().substring(args[0].length() + 1));
  53.             }
  54.         }
  55.     }
  56.  
  57.     public static void copy(File first, File second, String[] args){
  58.         if (first.exists()) {
  59.             ArrayList<String> list = new ArrayList<>(Arrays.asList(first.list()));
  60.             File[] arr = second.listFiles();
  61.             list.sort(Comparator.naturalOrder());
  62.             Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
  63.             for (File f : arr) {
  64.                 if (f.isDirectory()) {
  65.                     copy(new File(first, f.getName()), f, args);
  66.                 } else if (!list.contains(f.getName())) {
  67.                     flag = false;
  68.                     System.out.println("COPY " + f.toString().substring(args[1].length() + 1));
  69.                 } else {
  70.                     try {
  71.                         File file = new File(first, f.getName());
  72.                         byte[] bf = new byte[(int) f.length()], bd = new byte[(int) file.length()];
  73.                         FileInputStream stream = new FileInputStream(f);
  74.                         stream.read(bf);
  75.                         stream.close();
  76.                         stream = new FileInputStream(file);
  77.                         stream.read(bd);
  78.                         stream.close();
  79.                         if (!Arrays.equals(bf, bd)) {
  80.                             flag = false;
  81.                             System.out.println("COPY " + f.toString().substring(args[1].length() + 1));
  82.                         }
  83.                     } catch (IOException e) {
  84.                     }
  85.                 }
  86.             }
  87.         } else {
  88.             flag = false;
  89.             File[] arr = second.listFiles();
  90.             Arrays.sort(arr, Comparator.comparing(x -> x.getName()));
  91.             for (File f : arr) {
  92.                 System.out.println("COPY " + f.toString().substring(args[0].length() + 1));
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement