11package the .bytecode .club .bytecodeviewer .util ;
22
3- import java .io .ByteArrayOutputStream ;
4- import java .io .File ;
5- import java .io .FileInputStream ;
6- import java .io .FileOutputStream ;
7- import java .io .IOException ;
8- import java .io .InputStream ;
3+ import java .io .*;
4+ import java .nio .file .Files ;
5+ import java .nio .file .Path ;
6+ import java .nio .file .Paths ;
97import java .util .ArrayList ;
8+ import java .util .Enumeration ;
109import java .util .HashMap ;
1110import java .util .Map .Entry ;
1211import java .util .jar .JarOutputStream ;
1312import java .util .zip .ZipEntry ;
13+ import java .util .zip .ZipException ;
1414import java .util .zip .ZipInputStream ;
1515
1616import me .konloch .kontainer .io .DiskWriter ;
1717
18+ import org .apache .commons .compress .archivers .zip .ZipArchiveEntry ;
19+ import org .apache .commons .compress .archivers .zip .ZipFile ;
20+ import org .apache .commons .compress .utils .IOUtils ;
21+ import org .apache .commons .io .FilenameUtils ;
1822import org .objectweb .asm .ClassReader ;
1923import org .objectweb .asm .ClassWriter ;
2024import org .objectweb .asm .tree .ClassNode ;
@@ -76,10 +80,14 @@ public static void put(final File jarFile) throws IOException {
7680 e .printStackTrace ();
7781 }
7882 } else {
79- System .out .println (jarFile + ">" + name + ": Header does not start with CAFEBABE, ignoring." );
83+ if (!entry .isDirectory ())
84+ files .put (name , bytes );
85+ //System.out.println(jarFile + ">" + name + ": Header does not start with CAFEBABE, ignoring.");
8086 }
8187 }
8288
89+ } catch (ZipException e ) {
90+ //ignore cause apache unzip
8391 } catch (Exception e ) {
8492 new the .bytecode .club .bytecodeviewer .api .ExceptionUI (e );
8593 } finally {
@@ -89,7 +97,57 @@ public static void put(final File jarFile) throws IOException {
8997 jis .close ();
9098 container .files = files ;
9199 BytecodeViewer .files .add (container );
100+ }
101+
102+ public static void put2 (final File jarFile ) throws IOException {
103+ //TODO try zip libraries till one works, worst case import Sun's jarsigner code from JDK 7 re-sign the jar to rebuilt the CRC, should also rebuild the archive byte offsets
104+
105+ FileContainer container = new FileContainer (jarFile );
106+ HashMap <String , byte []> files = new HashMap <String , byte []>();
107+
108+
109+ Path path = jarFile .toPath ();
92110
111+ String fileBaseName = FilenameUtils .getBaseName (path .getFileName ().toString ());
112+ Path destFolderPath = Paths .get (path .getParent ().toString (), fileBaseName );
113+
114+ try (ZipFile zipFile = new ZipFile (jarFile ))
115+ {
116+ Enumeration <? extends ZipArchiveEntry > entries = zipFile .getEntries ();
117+ while (entries .hasMoreElements ()) {
118+ ZipArchiveEntry entry = entries .nextElement ();
119+ Path entryPath = destFolderPath .resolve (entry .getName ());
120+ String name = entry .getName ();
121+ if (entry .isDirectory ()) {
122+ //directory
123+ } else {
124+ try (InputStream in = zipFile .getInputStream (entry ))
125+ {
126+
127+ final byte [] bytes = getBytes (in );
128+ if (!name .endsWith (".class" )) {
129+ files .put (name , bytes );
130+ } else {
131+ String cafebabe = String .format ("%02X" , bytes [0 ]) + String .format ("%02X" , bytes [1 ]) + String .format ("%02X" , bytes [2 ]) + String .format ("%02X" , bytes [3 ]);
132+ if (cafebabe .toLowerCase ().equals ("cafebabe" )) {
133+ try {
134+ final ClassNode cn = getNode (bytes );
135+ container .classes .add (cn );
136+ } catch (Exception e ) {
137+ e .printStackTrace ();
138+ }
139+ } else {
140+ files .put (name , bytes );
141+ }
142+ }
143+
144+ }
145+ }
146+ }
147+ }
148+
149+ container .files = files ;
150+ BytecodeViewer .files .add (container );
93151 }
94152
95153
0 commit comments