Search in sources :

Example 66 with JarOutputStream

use of java.util.jar.JarOutputStream in project felix by apache.

the class StartStopBundleTest method createBundle.

private static File createBundle(String manifest, File tempDir) throws IOException {
    File f = File.createTempFile("felix-bundle", ".jar", tempDir);
    Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("utf-8")));
    mf.getMainAttributes().putValue("Manifest-Version", "1.0");
    mf.getMainAttributes().putValue(Constants.BUNDLE_ACTIVATOR, TestBundleActivator.class.getName());
    JarOutputStream os = new JarOutputStream(new FileOutputStream(f), mf);
    String path = TestBundleActivator.class.getName().replace('.', '/') + ".class";
    os.putNextEntry(new ZipEntry(path));
    InputStream is = TestBundleActivator.class.getClassLoader().getResourceAsStream(path);
    byte[] b = new byte[is.available()];
    is.read(b);
    is.close();
    os.write(b);
    os.close();
    return f;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 67 with JarOutputStream

use of java.util.jar.JarOutputStream in project felix by apache.

the class BundleCacheTest method createJar.

private void createJar(File source, File target) throws Exception {
    File tmp = File.createTempFile("bundle", ".jar", filesDir);
    JarOutputStream output;
    if (new File(source, "META-INF/MANIFEST.MF").isFile()) {
        output = new JarOutputStream(new FileOutputStream(tmp), new Manifest(new FileInputStream(new File(source, "META-INF/MANIFEST.MF"))));
    } else {
        output = new JarOutputStream(new FileOutputStream(tmp));
    }
    writeRecursive(source, "", output);
    output.close();
    target.delete();
    tmp.renameTo(target);
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 68 with JarOutputStream

use of java.util.jar.JarOutputStream in project jphp by jphp-compiler.

the class StandaloneCompiler method compileJar.

public void compileJar(File jarFile, File destinationDirectory, String mainClassName) throws IOException, InterruptedException {
    Manifest manifest = null;
    if (mainClassName != null) {
        manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_TITLE, Information.NAME + " Compiler");
        manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, Information.NAME);
        manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VERSION, Information.CORE_VERSION);
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClassName);
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, "jphp-runtime.jar");
    }
    try (FileOutputStream outputStream = new FileOutputStream(jarFile)) {
        JarOutputStream jarOutputStream = manifest != null ? new JarOutputStream(outputStream, manifest) : new JarOutputStream(outputStream);
        try {
            scan(destinationDirectory, new Callback<Boolean, File>() {

                @Override
                public Boolean call(File file) {
                    Path relPath = destinationDirectory.toPath().relativize(file.toPath());
                    String name = relPath.toString().replace('\\', '/');
                    try {
                        if (file.isFile()) {
                            JarEntry jarEntry = new JarEntry(name);
                            jarEntry.setTime(file.lastModified());
                            jarOutputStream.putNextEntry(jarEntry);
                            try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
                                byte[] buffer = new byte[1024 * 32];
                                while (true) {
                                    int count = in.read(buffer);
                                    if (count == -1) {
                                        break;
                                    }
                                    jarOutputStream.write(buffer, 0, count);
                                }
                                jarOutputStream.closeEntry();
                            }
                        } else if (file.isDirectory()) {
                            JarEntry jarEntry = new JarEntry(name);
                            jarEntry.setTime(file.lastModified());
                            jarOutputStream.putNextEntry(jarEntry);
                            jarOutputStream.closeEntry();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
                }
            });
        } catch (InterruptedException e) {
        // nop.
        }
        jarOutputStream.close();
    }
}
Also used : Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry)

Example 69 with JarOutputStream

use of java.util.jar.JarOutputStream in project xtext-eclipse by eclipse.

the class CreateJar method invoke.

@Override
public void invoke(IWorkflowContext ctx) {
    try {
        File binDirectory = new File(binPath + "/" + packagePath);
        if (!binDirectory.exists())
            throw new RuntimeException(binPath + "/" + packagePath);
        File targetDirectory = new File(targetDir);
        if (!targetDirectory.exists())
            throw new RuntimeException(targetDir);
        File zipFile = new File(targetDirectory, "testData.jar");
        if (!zipFile.exists())
            zipFile.createNewFile();
        JarOutputStream outputStream = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
        try {
            String packagePathInJar = packagePath + "/";
            for (File classFile : binDirectory.listFiles()) {
                if (classFile.isFile() && classFile.getName().endsWith(".class") && !classFile.getName().endsWith("$RemoveMe.class")) {
                    addToJar(packagePathInJar, classFile, outputStream);
                }
            }
            File rootBindDirectoy = new File(binPath);
            File classFileWithoutPackage = new File(rootBindDirectoy, "ClassWithDefaultPackage.class");
            if (classFileWithoutPackage.isFile()) {
                addToJar("", classFileWithoutPackage, outputStream);
            }
        } finally {
            outputStream.close();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 70 with JarOutputStream

use of java.util.jar.JarOutputStream in project xtext-eclipse by eclipse.

the class JavaProjectSetupUtil method jarInputStream.

public static InputStream jarInputStream(@SuppressWarnings("unchecked") Pair<String, InputStream>... entries) {
    try {
        ByteArrayOutputStream out2 = new ByteArrayOutputStream();
        JarOutputStream jo = new JarOutputStream(new BufferedOutputStream(out2));
        for (Pair<String, InputStream> entry : entries) {
            JarEntry je = new JarEntry(entry.getKey());
            jo.putNextEntry(je);
            ByteStreams.copy(entry.getValue(), jo);
        }
        jo.close();
        return new ByteArrayInputStream(out2.toByteArray());
    } catch (IOException e) {
        throw new WrappedException(e);
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedOutputStream(java.io.BufferedOutputStream)

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