From ad660815f32d2c1ae49f8a791aa107d52ad37390 Mon Sep 17 00:00:00 2001 From: Konloch Date: Wed, 14 Jul 2021 03:11:59 -0700 Subject: [PATCH] Improved Directory Importing --- .../resources/ResourceType.java | 2 + .../resources/importing/ImportResource.java | 47 ++++++++++++------- .../impl/DirectoryResourceImporter.java | 11 ++--- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceType.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceType.java index 22cfd213..723085c3 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceType.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/ResourceType.java @@ -10,6 +10,8 @@ import java.util.HashMap; public enum ResourceType { + //TODO tar/gzip? + CLASS_FILE(IconResources.classIcon, "class"), JAVA_ARCHIVE(IconResources.jarIcon, "jar", "war", "ear"), ZIP_ARCHIVE(IconResources.zipIcon, "zip"), diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/ImportResource.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/ImportResource.java index ee4be323..a5dbb8ef 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/ImportResource.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/ImportResource.java @@ -60,28 +60,12 @@ public class ImportResource implements Runnable continue; } - //check for zip archives - if (fn.endsWith(".jar") || fn.endsWith(".zip") || fn.endsWith(".war") || fn.endsWith(".ear")) - Import.ZIP.getImporter().open(file); - //check for classes - else if (fn.endsWith(".class")) + if (fn.endsWith(".class")) Import.CLASS.getImporter().open(file); - //check for XAPKs - else if (fn.endsWith(".xapk")) - Import.XAPK.getImporter().open(file); - - //check for APKs - else if (fn.endsWith(".apk")) - Import.APK.getImporter().open(file); - - //check for DEX - else if (fn.endsWith(".dex")) - Import.DEX.getImporter().open(file); - //everything else import as a resource - else + else if(!importFile(file)) Import.FILE.getImporter().open(file); } } @@ -97,4 +81,31 @@ public class ImportResource implements Runnable } catch (NullPointerException ignored) { } } } + + public static boolean importFile(File file) throws Exception + { + final String fn = file.getName(); + + //check for zip archives + if (fn.endsWith(".jar") || fn.endsWith(".zip") || fn.endsWith(".war") || fn.endsWith(".ear")) + Import.ZIP.getImporter().open(file); + + //check for XAPKs + else if (fn.endsWith(".xapk")) + Import.XAPK.getImporter().open(file); + + //check for APKs + else if (fn.endsWith(".apk")) + Import.APK.getImporter().open(file); + + //check for DEX + else if (fn.endsWith(".dex")) + Import.DEX.getImporter().open(file); + + //return false + else + return false; + + return true; + } } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java index ee99280e..fd153c03 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/DirectoryResourceImporter.java @@ -4,6 +4,7 @@ import org.apache.commons.io.FilenameUtils; import org.objectweb.asm.tree.ClassNode; import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.resources.importing.Import; +import the.bytecode.club.bytecodeviewer.resources.importing.ImportResource; import the.bytecode.club.bytecodeviewer.resources.importing.Importer; import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.util.JarUtils; @@ -61,12 +62,10 @@ public class DirectoryResourceImporter implements Importer final String fileName = child.getName(); //attempt to import archives automatically - if (fileName.endsWith(".jar") || fileName.endsWith(".zip") || fileName.endsWith(".war") || fileName.endsWith(".ear")) - Import.ZIP.getImporter().open(child); - else if (fileName.endsWith(".apk")) - Import.APK.getImporter().open(child); - else if (fileName.endsWith(".dex")) - Import.DEX.getImporter().open(child); + if(ImportResource.importFile(file)) + { + //let import resource handle it + } else if (fileName.endsWith(".class")) { byte[] bytes = Files.readAllBytes(Paths.get(child.getAbsolutePath()));