Example usage for java.util.jar JarOutputStream JarOutputStream

List of usage examples for java.util.jar JarOutputStream JarOutputStream

Introduction

In this page you can find the example usage for java.util.jar JarOutputStream JarOutputStream.

Prototype

public JarOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new JarOutputStream with no manifest.

Usage

From source file:com.headwire.aem.tooling.intellij.eclipse.stub.JarBuilder.java

public InputStream buildJar(final IFolder sourceDir) throws CoreException {

    ByteArrayOutputStream store = new ByteArrayOutputStream();

    JarOutputStream zos = null;//from   ww  w  .  j  a va2 s  . c om
    InputStream manifestInput = null;
    try {
        IResource manifestResource = sourceDir.findMember(JarFile.MANIFEST_NAME);
        if (manifestResource == null || manifestResource.getType() != IResource.FILE) {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    "No file named " + JarFile.MANIFEST_NAME + " found under " + sourceDir));
        }

        manifestInput = ((IFile) manifestResource).getContents();

        Manifest manifest = new Manifest(manifestInput);

        zos = new JarOutputStream(store);
        zos.setLevel(Deflater.NO_COMPRESSION);
        // manifest first
        final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(anEntry);
        manifest.write(zos);
        zos.closeEntry();
        zipDir(sourceDir, zos, "");
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(manifestInput);
    }

    return new ByteArrayInputStream(store.toByteArray());
}

From source file:com.izforge.izpack.installer.unpacker.Pack200FileUnpacker.java

/**
 * Unpacks a pack packFile.//w w  w  . ja  v  a  2  s  . c  om
 *
 * @param packFile            the pack packFile meta-data
 * @param packInputStream the pack input stream
 * @param target          the target
 * @throws IOException        for any I/O error
 * @throws InstallerException for any installer exception
 */
@Override
public void unpack(PackFile packFile, InputStream packInputStream, File target)
        throws IOException, InstallerException {
    InputStream in = IOUtils.buffer(packInputStream);
    JarOutputStream jarOut = null;

    try {
        jarOut = new JarOutputStream(getTarget(packFile, target));
        Pack200.Unpacker unpacker = createPack200Unpacker(packFile);
        unpacker.unpack(in, jarOut);
    } finally {
        IOUtils.closeQuietly(jarOut);
        IOUtils.closeQuietly(in);
    }

    postCopy(packFile);
}

From source file:org.apache.pluto.util.assemble.ear.EarAssembler.java

public void assembleInternal(AssemblerConfig config) throws UtilityException, IOException {

    File source = config.getSource();
    File dest = config.getDestination();

    JarInputStream earIn = new JarInputStream(new FileInputStream(source));
    JarOutputStream earOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(dest), BUFLEN));

    try {/*from   w w  w. j  a  v  a2  s. com*/

        JarEntry entry;

        // Iterate over entries in the EAR archive
        while ((entry = earIn.getNextJarEntry()) != null) {

            // If a war file is encountered, assemble it into a
            // ByteArrayOutputStream and write the assembled bytes
            // back to the EAR archive.
            if (entry.getName().toLowerCase().endsWith(".war")) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Assembling war file " + entry.getName());
                }

                // keep a handle to the AssemblySink so we can write out
                // JarEntry metadata and the bytes later.
                AssemblySink warBytesOut = getAssemblySink(config, entry);
                JarOutputStream warOut = new JarOutputStream(warBytesOut);

                JarStreamingAssembly.assembleStream(new JarInputStream(earIn), warOut,
                        config.getDispatchServletClass());

                JarEntry warEntry = new JarEntry(entry);

                // Write out the assembled JarEntry metadata
                warEntry.setSize(warBytesOut.getByteCount());
                warEntry.setCrc(warBytesOut.getCrc());
                warEntry.setCompressedSize(-1);
                earOut.putNextEntry(warEntry);

                // Write out the assembled WAR file to the EAR
                warBytesOut.writeTo(earOut);

                earOut.flush();
                earOut.closeEntry();
                earIn.closeEntry();

            } else {

                earOut.putNextEntry(entry);
                IOUtils.copy(earIn, earOut);

                earOut.flush();
                earOut.closeEntry();
                earIn.closeEntry();

            }
        }

    } finally {

        earOut.close();
        earIn.close();

    }
}

From source file:JarMaker.java

public static void RealPackUtilityJar(String s_src, String s_dest) throws IOException {

    URL _src, _dest;/*  w w w .  ja v  a  2s . c o  m*/

    File f_src = new File(s_src);
    if (!f_src.isDirectory())
        throw new IOException(s_src + " is not a directory");
    _src = f_src.toURL();
    _dest = new File(s_dest).toURL();

    int path_l = s_dest.lastIndexOf("/");
    if (path_l > 0) {
        File dir = new File(s_dest.substring(0, path_l));
        if (!dir.exists())
            dir.mkdirs();
    }

    JarOutputStream jout = new JarOutputStream(new FileOutputStream(_dest.getFile()));
    // put all into the jar...
    add(jout, new File(_src.getFile()), "");
    jout.close();
}

From source file:org.commonjava.maven.galley.filearc.internal.ZipPublish.java

private Boolean writeArchive(final File dest, final String path) {
    final boolean isJar = isJarOperation();
    FileOutputStream fos = null;//from  ww  w  . j  av  a  2s .  co m
    ZipOutputStream zos = null;
    ZipFile zf = null;
    try {
        fos = new FileOutputStream(dest);
        zos = isJar ? new JarOutputStream(fos) : new ZipOutputStream(fos);

        zf = isJar ? new JarFile(dest) : new ZipFile(dest);

        final ZipEntry entry = zf.getEntry(path);
        zos.putNextEntry(entry);
        copy(stream, zos);

        return true;
    } catch (final IOException e) {
        error = new TransferException("Failed to write path: %s to NEW archive: %s. Reason: %s", e, path, dest,
                e.getMessage());
    } finally {
        closeQuietly(zos);
        closeQuietly(fos);
        if (zf != null) {
            //noinspection EmptyCatchBlock
            try {
                zf.close();
            } catch (final IOException e) {
            }
        }
        closeQuietly(stream);
    }

    return false;
}

From source file:yrun.YarnRunnerUtil.java

private static Path constructJar(File sourceFile) throws IOException {
    if (sourceFile.isDirectory()) {
        File file = File.createTempFile(TMP_YARNRUNNER_JOB, JAR);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        JarOutputStream jarOut = new JarOutputStream(outputStream);
        for (File f : sourceFile.listFiles()) {
            pack(sourceFile, f, jarOut);
        }/*from  w  w  w.j ava 2  s. c o  m*/
        jarOut.close();
        file.deleteOnExit();
        return new Path(file.toURI());
    }
    throw new RuntimeException("File [" + sourceFile + "] is not a directory.");
}

From source file:org.apache.maven.plugins.shade.resource.ServiceResourceTransformerTest.java

@Test
public void relocatedClasses() throws Exception {
    SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null,
            Arrays.asList("org.foo.exclude.*"));
    List<Relocator> relocators = Lists.<Relocator>newArrayList(relocator);

    String content = "org.foo.Service\norg.foo.exclude.OtherService\n";
    byte[] contentBytes = content.getBytes("UTF-8");
    InputStream contentStream = new ByteArrayInputStream(contentBytes);
    String contentResource = "META-INF/services/org.foo.something.another";
    String contentResourceShaded = "META-INF/services/borg.foo.something.another";

    ServicesResourceTransformer xformer = new ServicesResourceTransformer();
    xformer.processResource(contentResource, contentStream, relocators);
    contentStream.close();/*from   w w  w .  j a v  a 2  s. c  o m*/

    File tempJar = File.createTempFile("shade.", ".jar");
    tempJar.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempJar);
    JarOutputStream jos = new JarOutputStream(fos);
    try {
        xformer.modifyOutputStream(jos, false);
        jos.close();
        jos = null;
        JarFile jarFile = new JarFile(tempJar);
        JarEntry jarEntry = jarFile.getJarEntry(contentResourceShaded);
        assertNotNull(jarEntry);
        InputStream entryStream = jarFile.getInputStream(jarEntry);
        try {
            String xformedContent = IOUtils.toString(entryStream, "utf-8");
            assertEquals("borg.foo.Service" + System.getProperty("line.separator")
                    + "org.foo.exclude.OtherService" + System.getProperty("line.separator"), xformedContent);
        } finally {
            IOUtils.closeQuietly(entryStream);
            jarFile.close();
        }
    } finally {
        if (jos != null) {
            IOUtils.closeQuietly(jos);
        }
        tempJar.delete();
    }
}

From source file:rubah.tools.ReadWriteTool.java

@Override
public void processJar() throws IOException {

    if (this.parameters instanceof ReadWriteParameters)
        this.outFile = ((ReadWriteParameters) this.parameters).outJar;

    this.outJarStream = new JarOutputStream(new FileOutputStream(this.outFile));

    //      try {
    super.processJar();
    this.endProcess();
    //      } catch (Throwable e) {
    //         throw new Error(e);
    //      } finally {
    this.outJarStream.close();
    //      }//from  www . j a v a2 s  . c  o  m
}

From source file:org.apache.apex.malhar.sql.schema.TupleSchemaRegistry.java

public String generateCommonJar() throws IOException {
    File file = File.createTempFile("schemaSQL", ".jar");

    FileSystem fs = FileSystem.newInstance(file.toURI(), new Configuration());
    FSDataOutputStream out = fs.create(new Path(file.getAbsolutePath()));
    JarOutputStream jout = new JarOutputStream(out);

    for (Schema schema : schemas.values()) {
        jout.putNextEntry(new ZipEntry(schema.fqcn.replace(".", "/") + ".class"));
        jout.write(schema.beanClassBytes);
        jout.closeEntry();//from w w  w .j  av  a2s  . co m
    }

    jout.close();
    out.close();

    return file.getAbsolutePath();
}

From source file:org.mule.util.JarUtils.java

public static void createJarFileEntries(File jarFile, LinkedHashMap entries) throws Exception {
    JarOutputStream jarStream = null;
    FileOutputStream fileStream = null;

    if (jarFile != null) {
        logger.debug("Creating jar file " + jarFile.getAbsolutePath());

        try {//from  ww  w  . ja va  2 s . co  m
            fileStream = new FileOutputStream(jarFile);
            jarStream = new JarOutputStream(fileStream);

            if (entries != null && !entries.isEmpty()) {
                Iterator iter = entries.keySet().iterator();
                while (iter.hasNext()) {
                    String jarFilePath = (String) iter.next();
                    Object content = entries.get(jarFilePath);

                    JarEntry entry = new JarEntry(jarFilePath);
                    jarStream.putNextEntry(entry);

                    logger.debug("Adding jar entry " + jarFilePath + " to " + jarFile.getAbsolutePath());

                    if (content instanceof String) {
                        writeJarEntry(jarStream, ((String) content).getBytes());
                    } else if (content instanceof byte[]) {
                        writeJarEntry(jarStream, (byte[]) content);
                    } else if (content instanceof File) {
                        writeJarEntry(jarStream, (File) content);
                    }
                }
            }

            jarStream.flush();
            fileStream.getFD().sync();
        } finally {
            if (jarStream != null) {
                try {
                    jarStream.close();
                } catch (Exception jarNotClosed) {
                    logger.debug(jarNotClosed);
                }
            }
            if (fileStream != null) {
                try {
                    fileStream.close();
                } catch (Exception fileNotClosed) {
                    logger.debug(fileNotClosed);
                }
            }
        }
    }
}