Search in sources :

Example 81 with JarOutputStream

use of java.util.jar.JarOutputStream in project jdk8u_jdk by JetBrains.

the class UnpackerMemoryTest method main.

public static void main(String[] args) throws Exception {
    String name = "foo";
    File packFile = new File(name + Utils.PACK_FILE_EXT);
    createPackFile(packFile);
    if (!packFile.exists()) {
        throw new RuntimeException(packFile + " not found");
    }
    File jarOut = new File(name + ".out");
    for (int i = 0; i < 2000; i++) {
        JarOutputStream jarOS = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(jarOut);
            jarOS = new JarOutputStream(fos);
            System.out.println("Unpacking[" + i + "]" + packFile);
            Utils.unpackn(packFile, jarOS);
        } finally {
            Utils.close(jarOS);
            Utils.close(fos);
        }
    }
    Utils.cleanup();
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 82 with JarOutputStream

use of java.util.jar.JarOutputStream in project bytecode-viewer by Konloch.

the class JarUtils method saveAsJar.

public static void saveAsJar(Map<String, byte[]> nodeList, String path) {
    try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) {
        ArrayList<String> noDupe = new ArrayList<String>();
        for (Entry<String, byte[]> entry : nodeList.entrySet()) {
            String name = entry.getKey();
            if (!noDupe.contains(name)) {
                noDupe.add(name);
                out.putNextEntry(new ZipEntry(name));
                out.write(entry.getValue());
                out.closeEntry();
            }
        }
        for (FileContainer container : BytecodeViewer.files) for (Entry<String, byte[]> entry : container.files.entrySet()) {
            String filename = entry.getKey();
            if (!filename.startsWith("META-INF")) {
                if (!noDupe.contains(filename)) {
                    noDupe.add(filename);
                    out.putNextEntry(new ZipEntry(filename));
                    out.write(entry.getValue());
                    out.closeEntry();
                }
            }
        }
        noDupe.clear();
    } catch (IOException e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) Entry(java.util.Map.Entry) ZipEntry(java.util.zip.ZipEntry)

Example 83 with JarOutputStream

use of java.util.jar.JarOutputStream in project bytecode-viewer by Konloch.

the class JarUtils method saveAsJarClassesOnly.

/**
	 * Saves a jar without the manifest
	 * @param nodeList The loaded ClassNodes
	 * @param path the exact jar output path
	 */
public static void saveAsJarClassesOnly(ArrayList<ClassNode> nodeList, String path) {
    try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) {
        ArrayList<String> noDupe = new ArrayList<String>();
        for (ClassNode cn : nodeList) {
            ClassWriter cw = new ClassWriter(0);
            cn.accept(cw);
            String name = cn.name + ".class";
            if (!noDupe.contains(name)) {
                noDupe.add(name);
                out.putNextEntry(new ZipEntry(name));
                out.write(cw.toByteArray());
                out.closeEntry();
            }
        }
        noDupe.clear();
    } catch (IOException e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ClassWriter(org.objectweb.asm.ClassWriter)

Example 84 with JarOutputStream

use of java.util.jar.JarOutputStream in project bytecode-viewer by Konloch.

the class JarUtils method saveAsJar.

/**
	 * Saves as jar with manifest
	 * @param nodeList the loaded ClassNodes
	 * @param path the exact path of the output jar file
	 * @param manifest the manifest contents
	 */
public static void saveAsJar(ArrayList<ClassNode> nodeList, String path, String manifest) {
    try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) {
        for (ClassNode cn : nodeList) {
            ClassWriter cw = new ClassWriter(0);
            cn.accept(cw);
            out.putNextEntry(new ZipEntry(cn.name + ".class"));
            out.write(cw.toByteArray());
            out.closeEntry();
        }
        out.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
        out.write((manifest.trim() + "\r\n\r\n").getBytes());
        out.closeEntry();
        for (FileContainer container : BytecodeViewer.files) for (Entry<String, byte[]> entry : container.files.entrySet()) {
            String filename = entry.getKey();
            if (!filename.startsWith("META-INF")) {
                out.putNextEntry(new ZipEntry(filename));
                out.write(entry.getValue());
                out.closeEntry();
            }
        }
    } catch (IOException e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ClassWriter(org.objectweb.asm.ClassWriter) Entry(java.util.Map.Entry) ZipEntry(java.util.zip.ZipEntry)

Example 85 with JarOutputStream

use of java.util.jar.JarOutputStream in project bytecode-viewer by Konloch.

the class JarUtils method saveAsJarClassesOnly.

public static void saveAsJarClassesOnly(Map<String, byte[]> nodeList, String path) {
    try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) {
        ArrayList<String> noDupe = new ArrayList<String>();
        for (Entry<String, byte[]> cn : nodeList.entrySet()) {
            String name = cn.getKey();
            if (!noDupe.contains(name)) {
                noDupe.add(name);
                out.putNextEntry(new ZipEntry(name));
                out.write(cn.getValue());
                out.closeEntry();
            }
        }
        noDupe.clear();
    } catch (IOException e) {
        new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)485 FileOutputStream (java.io.FileOutputStream)308 File (java.io.File)265 JarEntry (java.util.jar.JarEntry)194 Manifest (java.util.jar.Manifest)140 IOException (java.io.IOException)130 ZipEntry (java.util.zip.ZipEntry)116 InputStream (java.io.InputStream)89 FileInputStream (java.io.FileInputStream)84 JarFile (java.util.jar.JarFile)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 ByteArrayInputStream (java.io.ByteArrayInputStream)55 Test (org.junit.Test)55 BufferedOutputStream (java.io.BufferedOutputStream)47 Path (java.nio.file.Path)42 JarInputStream (java.util.jar.JarInputStream)41 OutputStream (java.io.OutputStream)36 Attributes (java.util.jar.Attributes)36 ArrayList (java.util.ArrayList)35 Map (java.util.Map)27