From f32e5225cbcc748b6e3d17e8cedccb4cf5d5c61b Mon Sep 17 00:00:00 2001 From: bly Date: Wed, 4 Jan 2023 03:20:21 +0100 Subject: [PATCH] fixed A bug where it just randomly crashes and added the ability to delete duplicate files with -d paramater --- .idea/artifacts/Sherly_jar.xml | 8 +++++ .idea/workspace.xml | 64 +++++++++++++++++++++++++++++++--- META-INF/MANIFEST.MF | 3 ++ src/ConsoleColors.java | 1 + src/META-INF/MANIFEST.MF | 3 ++ src/Main.java | 49 +++++++++++++++++++++++++- src/ThreadedCompare.java | 10 ++++-- 7 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 .idea/artifacts/Sherly_jar.xml create mode 100644 META-INF/MANIFEST.MF create mode 100644 src/META-INF/MANIFEST.MF diff --git a/.idea/artifacts/Sherly_jar.xml b/.idea/artifacts/Sherly_jar.xml new file mode 100644 index 0000000..139526d --- /dev/null +++ b/.idea/artifacts/Sherly_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/Sherly_jar + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index aec1957..384e82a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,10 +1,21 @@ + + + + + - + + + + + + + \ No newline at end of file diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/ConsoleColors.java b/src/ConsoleColors.java index 075d762..24b2f11 100644 --- a/src/ConsoleColors.java +++ b/src/ConsoleColors.java @@ -10,5 +10,6 @@ public class ConsoleColors { public static final String CYAN_BOLD = "\033[1;36m"; // CYAN + public static final String RED_BOLD = "\033[1;31m"; // RED } diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java index 1cdfa3f..3b7d3cf 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -5,6 +6,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Scanner; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -17,7 +19,7 @@ public class Main { public static void main(String[] args) throws InterruptedException { boolean doTheColorThingy = false; boolean showProgress = false; - + boolean deleteDups = false; boolean recordFolder = false; List paths = new ArrayList<>(); @@ -32,6 +34,7 @@ public class Main { if (i.equalsIgnoreCase("-c") || i.equalsIgnoreCase("-color")) { doTheColorThingy = true;} if (i.equalsIgnoreCase("-p") || i.equalsIgnoreCase("-progress")) { showProgress = true;} if (i.equalsIgnoreCase("-f") || i.equalsIgnoreCase("-folder")) { recordFolder = true;} + if (i.equalsIgnoreCase("-d") || i.equalsIgnoreCase("-delete")) { deleteDups = true;} } @@ -118,8 +121,52 @@ public class Main { } + if (deleteDups) { + List allTheFilesWillBeDeleted = new ArrayList<>(); + long bytes = 0; + + for (String md5: fileMap.keySet()) { + Main.fileMap.get(md5).remove(0); + for (Path file: Main.fileMap.get(md5)) { + bytes += file.toFile().length(); + } + allTheFilesWillBeDeleted.addAll(Main.fileMap.get(md5)); + + } + + + + ask(doTheColorThingy, bytes, allTheFilesWillBeDeleted); + + } + + } + public static void ask(boolean color, long bytes, List deleteThem) { + if (color) { + System.out.println(ConsoleColors.RED_BOLD + (bytes / 8000000) + " unnecessary MB found, do you want to Delete them? Y / N" + ConsoleColors.RESET); + } else { + System.out.println((bytes / 8000000) + " unnecessary MB found, do you want to Delete them? Y / N"); + } + Scanner input = new Scanner(System.in); + String answer = input.next(); + if (answer.toLowerCase().contains("y")) { + delete(deleteThem); + input.close(); + + } else if (answer.toLowerCase().contains("n")) { + return; + } else { + ask(color, bytes, deleteThem); + } + input.close(); } + public static void delete(List deleteThem) { + for (Path file : deleteThem) { + file.toFile().delete(); + } + } + } diff --git a/src/ThreadedCompare.java b/src/ThreadedCompare.java index ca2e0ab..795c37f 100644 --- a/src/ThreadedCompare.java +++ b/src/ThreadedCompare.java @@ -27,8 +27,14 @@ public class ThreadedCompare extends Thread { //here it is trying to add the values in the HashMap so everything is nice and clear Main.fileMap.putIfAbsent(getMD5Sum(file1.toFile()), bothList); - Main.fileMap.get(getMD5Sum(file1.toFile())).removeAll(bothList); - Main.fileMap.get(getMD5Sum(file1.toFile())).addAll(bothList); + + if (!bothList.isEmpty()) { + + Main.fileMap.get(getMD5Sum(file1.toFile())).remove(file1); + Main.fileMap.get(getMD5Sum(file1.toFile())).remove(file2); + Main.fileMap.get(getMD5Sum(file1.toFile())).addAll(bothList); + } + } } catch (IOException e) {