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
branch_1_1
hoenicke 24 years ago
parent 4888c5f98c
commit 0785a7cd21
  1. 121
      jode/jode/decompiler/Main.java

@ -28,7 +28,9 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.Enumeration;
import gnu.getopt.LongOpt; import gnu.getopt.LongOpt;
import gnu.getopt.Getopt; import gnu.getopt.Getopt;
@ -138,6 +140,60 @@ public class Main extends Options {
return true; 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) { public static void main(String[] params) {
if (params.length == 0) { if (params.length == 0) {
usage(); usage();
@ -246,7 +302,7 @@ public class Main extends Options {
} }
if (errorInParams) if (errorInParams)
return; return;
ClassInfo.setClassPath(classPath.toString()); ClassInfo.setClassPath(classPath);
ImportHandler imports = new ImportHandler(importPackageLimit, ImportHandler imports = new ImportHandler(importPackageLimit,
importClassLimit); importClassLimit);
@ -268,51 +324,32 @@ public class Main extends Options {
} }
for (int i= g.getOptind(); i< params.length; i++) { for (int i= g.getOptind(); i< params.length; i++) {
try { try {
ClassInfo clazz; if ((params[i].endsWith(".jar") || params[i].endsWith(".zip"))
try { && new File(params[i]).isFile()) {
clazz = ClassInfo.forName(params[i]); /* The user obviously wants to decompile a jar/zip file.
} catch (IllegalArgumentException ex) { * Lets do him a pleasure and allow this.
GlobalOptions.err.println */
("`"+params[i]+"' is not a class name"); ClassInfo.setClassPath(params[i]
continue; + SearchPath.altPathSeparatorChar
} + classPath);
if (skipClass(clazz)) Enumeration enum = new ZipFile(params[i]).entries();
continue; while (enum.hasMoreElements()) {
String entry
String filename = = ((ZipEntry) enum.nextElement()).getName();
params[i].replace('.', File.separatorChar)+".java"; if (entry.endsWith(".class")) {
if (destZip != null) { entry = entry.substring(0, entry.length() - 6)
writer.flush(); .replace('/', '.');
destZip.putNextEntry(new ZipEntry(filename)); decompileClass(entry, destZip, destDir,
} else if (destDir != null) { writer, imports);
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);
} }
ClassInfo.setClassPath(classPath);
GlobalOptions.err.println(params[i]); } else
decompileClass(params[i], destZip, destDir,
ClassAnalyzer clazzAna = new ClassAnalyzer(clazz, imports); writer, 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) { } catch (IOException ex) {
GlobalOptions.err.println GlobalOptions.err.println
("Can't write source of "+params[i]+"."); ("Can't read zip file " + params[i] + ".");
GlobalOptions.err.println("Check the permissions.");
ex.printStackTrace(GlobalOptions.err); ex.printStackTrace(GlobalOptions.err);
} }
} }

Loading…
Cancel
Save