diff --git a/src/main/java/jd/core/Decompiler.java b/src/main/java/jd/core/Decompiler.java index 900ed93..ccdc99c 100644 --- a/src/main/java/jd/core/Decompiler.java +++ b/src/main/java/jd/core/Decompiler.java @@ -3,6 +3,7 @@ import java.io.*; import java.util.Scanner; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.Map; import java.util.HashMap; @@ -19,7 +20,7 @@ public Decompiler() { } public int decompile(String jarPath, String outPath) throws DecompilerException, IOException { - Map pathToSrc = decompile(jarPath); + Map pathToSrc = decompile(jarPath); if (outPath == null) { outPath = jarPath.replaceAll("\\.jar$", "") + ".src"; @@ -35,26 +36,32 @@ public int decompile(String jarPath, String outPath) throws DecompilerException, } } - public int decompileToDir(String outDir, Map pathToSrc) throws FileNotFoundException { - for (Map.Entry entry : pathToSrc.entrySet()) { + public int decompileToDir(String outDir, Map pathToSrc) throws IOException { + for (Map.Entry entry : pathToSrc.entrySet()) { String fileName = entry.getKey(); File file = new File(outDir, fileName); file.getParentFile().mkdirs(); - PrintWriter out = new PrintWriter(file); - out.print(entry.getValue()); - out.close(); + + ByteArrayOutputStream os = entry.getValue(); + + FileOutputStream out = new FileOutputStream(file); + os.writeTo(out); + out.close(); + } return pathToSrc.size(); } - public int decompileToZip(String zipName, Map entries) throws IOException { + public int decompileToZip(String zipName, Map entries) throws IOException { ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipName)); - PrintWriter printOut = new PrintWriter(zipOut); - for(Map.Entry entry : entries.entrySet()) { + for(Map.Entry entry : entries.entrySet()) { zipOut.putNextEntry(new ZipEntry(entry.getKey())); - printOut.print(entry.getValue()); - printOut.flush(); + + ByteArrayOutputStream os = entry.getValue(); + + os.writeTo(zipOut); + zipOut.closeEntry(); } @@ -71,10 +78,11 @@ public String decompileClass(String jarPath, String internalClassName) throws De return decompiled; } - public Map decompile(String jarPath) throws DecompilerException, IOException { + public Map decompile(String jarPath) throws DecompilerException, IOException { + ZipFile zFile = new ZipFile(jarPath); ZipInputStream zip = new ZipInputStream(new FileInputStream(jarPath)); ZipEntry ze; - Map pathToSrc = new HashMap(); + Map pathToSrc = new HashMap(); CaseInsensitiveFilePathSet caseInsensitiveSet = new CaseInsensitiveFilePathSet(); Scanner in = new Scanner(zip); @@ -92,14 +100,26 @@ public Map decompile(String jarPath) throws DecompilerException, } } caseInsensitiveSet.add(javaPath); - pathToSrc.put(javaPath, decompiler.decompile(jarPath, classPath)); + + String sourceCode = decompiler.decompile(jarPath, classPath); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + os.write(sourceCode.getBytes()); + + pathToSrc.put(javaPath, os); } } else if( !ze.isDirectory() ) { - StringBuilder entry = new StringBuilder(); - while(in.hasNextLine()) { - entry.append(in.nextLine()).append('\n'); + + InputStream is = zFile.getInputStream(ze); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + int i; + while ((i = is.read()) != -1) + { + os.write(i); } - pathToSrc.put(ze.getName(), entry.toString()); + + pathToSrc.put(ze.getName(), os); } }