From e1915328fb613f032cafd2e34d3f2776d4242814 Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 2 Oct 2024 11:20:33 -0600 Subject: [PATCH] Continued Temp File API --- .../club/bytecodeviewer/util/TempFile.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java index efb66c2e..f372ec9c 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/TempFile.java @@ -1,6 +1,7 @@ package the.bytecode.club.bytecodeviewer.util; import java.io.File; +import java.util.HashSet; import static the.bytecode.club.bytecodeviewer.Constants.TEMP_DIRECTORY; @@ -13,12 +14,19 @@ public class TempFile private final File parent; private final File file; private final String filePath; + private final HashSet createdFilePaths = new HashSet<>(); public TempFile(File file) { this.parent = file.getParentFile(); this.file = file; this.filePath = file.getAbsolutePath(); + this.createdFilePaths.add(file.getAbsolutePath()); + } + + public File getParent() + { + return parent; } public File getFile() @@ -31,6 +39,23 @@ public class TempFile return filePath; } + public void delete() + { + //delete all the items + for(String path : createdFilePaths) + { + File toDelete = new File(path); + + toDelete.delete(); + } + + //delete parent if it's not the main temp directory + if(!getParent().getAbsolutePath().equalsIgnoreCase(new File(TEMP_DIRECTORY).getAbsolutePath())) + { + getParent().delete(); + } + } + public File createFileFromExtension(String extension) { File file; @@ -40,6 +65,8 @@ public class TempFile { } + this.createdFilePaths.add(file.getAbsolutePath()); + return file; }