From 0785a7cd21fdc2fe484c88625f601cd3dab60b42 Mon Sep 17 00:00:00 2001 From: hoenicke Date: Mon, 29 Jan 2001 19:46:12 +0000 Subject: [PATCH] Support decompiling complete Zip files git-svn-id: https://svn.code.sf.net/p/jode/code/branches/branch_1_1@1286 379699f6-c40d-0410-875b-85095c16579e --- jode/jode/decompiler/Main.java | 123 +++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 43 deletions(-) diff --git a/jode/jode/decompiler/Main.java b/jode/jode/decompiler/Main.java index 8b84c48..137d35b 100644 --- a/jode/jode/decompiler/Main.java +++ b/jode/jode/decompiler/Main.java @@ -28,7 +28,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.zip.ZipOutputStream; +import java.util.zip.ZipFile; import java.util.zip.ZipEntry; +import java.util.Enumeration; import gnu.getopt.LongOpt; import gnu.getopt.Getopt; @@ -138,6 +140,60 @@ public class Main extends Options { return true; } + public static void decompileClass(String className, + ZipOutputStream destZip, String destDir, + TabbedPrintWriter writer, + ImportHandler imports) { + try { + ClassInfo clazz; + try { + clazz = ClassInfo.forName(className); + } catch (IllegalArgumentException ex) { + GlobalOptions.err.println + ("`"+className+"' is not a class name"); + return; + } + if (skipClass(clazz)) + return; + + String filename = + className.replace('.', File.separatorChar)+".java"; + if (destZip != null) { + writer.flush(); + destZip.putNextEntry(new ZipEntry(filename)); + } else if (destDir != null) { + File file = new File (destDir, filename); + File directory = new File(file.getParent()); + if (!directory.exists() && !directory.mkdirs()) { + GlobalOptions.err.println + ("Could not create directory " + + directory.getPath() + ", check permissions."); + } + writer = new TabbedPrintWriter + (new BufferedOutputStream(new FileOutputStream(file)), + imports, false); + } + + GlobalOptions.err.println(className); + + ClassAnalyzer clazzAna = new ClassAnalyzer(clazz, imports); + clazzAna.dumpJavaFile(writer); + + if (destZip != null) { + writer.flush(); + destZip.closeEntry(); + } else if (destDir != null) + writer.close(); + /* Now is a good time to clean up */ + System.gc(); + } catch (IOException ex) { + GlobalOptions.err.println + ("Can't write source of "+className+"."); + GlobalOptions.err.println("Check the permissions."); + ex.printStackTrace(GlobalOptions.err); + } + } + public static void main(String[] params) { if (params.length == 0) { usage(); @@ -246,7 +302,7 @@ public class Main extends Options { } if (errorInParams) return; - ClassInfo.setClassPath(classPath.toString()); + ClassInfo.setClassPath(classPath); ImportHandler imports = new ImportHandler(importPackageLimit, importClassLimit); @@ -268,51 +324,32 @@ public class Main extends Options { } for (int i= g.getOptind(); i< params.length; i++) { try { - ClassInfo clazz; - try { - clazz = ClassInfo.forName(params[i]); - } catch (IllegalArgumentException ex) { - GlobalOptions.err.println - ("`"+params[i]+"' is not a class name"); - continue; - } - if (skipClass(clazz)) - continue; - - String filename = - params[i].replace('.', File.separatorChar)+".java"; - if (destZip != null) { - writer.flush(); - destZip.putNextEntry(new ZipEntry(filename)); - } else if (destDir != null) { - File file = new File (destDir, filename); - File directory = new File(file.getParent()); - if (!directory.exists() && !directory.mkdirs()) { - GlobalOptions.err.println - ("Could not create directory " - + directory.getPath() + ", check permissions."); + if ((params[i].endsWith(".jar") || params[i].endsWith(".zip")) + && new File(params[i]).isFile()) { + /* The user obviously wants to decompile a jar/zip file. + * Lets do him a pleasure and allow this. + */ + ClassInfo.setClassPath(params[i] + + SearchPath.altPathSeparatorChar + + classPath); + Enumeration enum = new ZipFile(params[i]).entries(); + while (enum.hasMoreElements()) { + String entry + = ((ZipEntry) enum.nextElement()).getName(); + if (entry.endsWith(".class")) { + entry = entry.substring(0, entry.length() - 6) + .replace('/', '.'); + decompileClass(entry, destZip, destDir, + writer, imports); + } } - writer = new TabbedPrintWriter - (new BufferedOutputStream(new FileOutputStream(file)), - imports, false); - } - - GlobalOptions.err.println(params[i]); - - ClassAnalyzer clazzAna = new ClassAnalyzer(clazz, imports); - clazzAna.dumpJavaFile(writer); - - if (destZip != null) { - writer.flush(); - destZip.closeEntry(); - } else if (destDir != null) - writer.close(); - /* Now is a good time to clean up */ - System.gc(); + ClassInfo.setClassPath(classPath); + } else + decompileClass(params[i], destZip, destDir, + writer, imports); } catch (IOException ex) { GlobalOptions.err.println - ("Can't write source of "+params[i]+"."); - GlobalOptions.err.println("Check the permissions."); + ("Can't read zip file " + params[i] + "."); ex.printStackTrace(GlobalOptions.err); } }