Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- public class Sync {
- private static boolean identical;
- public static void main(String[] args) {
- File S = new File(args[0]), D = new File(args[1]);
- identical = true;
- sync(S, D, "DELETE ");
- sync(D, S, "COPY ");
- if (identical) {
- System.out.println("IDENTICAL");
- }
- }
- private static void sync(File s, File d, String command) {
- if (s.exists()) {
- ArrayList<String> sf = new ArrayList<>(Arrays.asList(s.list()));
- File[] files = d.listFiles();
- sf.sort(Comparator.naturalOrder());
- Arrays.sort(files, Comparator.comparing(File::getName));
- for (File f : files) {
- if (f.isDirectory()) {
- sync(new File(s, f.getName()), f, command);
- } else if (!sf.contains(f.getName())) {
- identical = false;
- System.out.println(command + f.toString().substring(2));
- } else if (new File(s, f.getName()).length() != f.length()) {
- try {
- File mFile = new File(s, f.getName());
- byte[] bf = new byte[(int) f.length()], bd = new byte[(int) mFile.length()];
- FileInputStream mFileInputStream = new FileInputStream(f);
- mFileInputStream.read(bf);
- mFileInputStream.close();
- mFileInputStream = new FileInputStream(mFile);
- mFileInputStream.read(bd);
- mFileInputStream.close();
- if (!Arrays.equals(bf, bd)) {
- identical = false;
- System.out.println(command + f.toString().substring(2));
- }
- } catch (Throwable t) {
- }
- }
- }
- } else {
- identical = false;
- File[] files = d.listFiles();
- Arrays.sort(files, Comparator.comparing(File::getName));
- for (File f : files) {
- System.out.println(command + f.toString().substring(2));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement