Example usage for java.util.jar JarOutputStream putNextEntry

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

Introduction

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

Prototype

public void putNextEntry(ZipEntry ze) throws IOException 

Source Link

Document

Begins writing a new JAR file entry and positions the stream to the start of the entry data.

Usage

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();//w w  w  . j  a v  a2  s  .  c om
    }

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

    return file.getAbsolutePath();
}

From source file:com.streamsets.datacollector.cluster.TestTarFileCreator.java

private File createJar(File parentDir) throws IOException {
    if (parentDir.isFile()) {
        parentDir.delete();//from   w w w .j  av a 2s . c o  m
    }
    parentDir.mkdirs();
    Assert.assertTrue(parentDir.isDirectory());
    String uuid = UUID.randomUUID().toString();
    File jar = new File(parentDir, uuid + ".jar");
    JarOutputStream out = new JarOutputStream(new FileOutputStream(jar));
    JarEntry entry = new JarEntry("sample.txt");
    out.putNextEntry(entry);
    out.write(uuid.getBytes(StandardCharsets.UTF_8));
    out.closeEntry();
    out.close();
    return jar;
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static void writeSignatureBlock(byte[] sigBlock, SignatureType signatureType, String signatureName,
        JarOutputStream jos) throws IOException {

    // Block's extension depends on signature type
    String extension = null;//  w ww . jav a2 s .co  m

    if (signatureType == SHA1_DSA) {
        extension = DSA_SIG_BLOCK_EXT;
    } else {
        extension = RSA_SIG_BLOCK_EXT;
    }

    // Signature block entry
    JarEntry bkJarEntry = new JarEntry(
            MessageFormat.format(METAINF_FILE_LOCATION, signatureName, extension).toUpperCase());
    jos.putNextEntry(bkJarEntry);

    // Write content
    ByteArrayInputStream bais = new ByteArrayInputStream(sigBlock);

    byte[] buffer = new byte[2048];
    int read = -1;

    while ((read = bais.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }

    jos.closeEntry();
}

From source file:com.glaf.core.util.ZipUtils.java

public static byte[] toZipBytes(Map<String, byte[]> zipMap) {
    byte[] bytes = null;
    InputStream inputStream = null;
    BufferedInputStream bis = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    JarOutputStream jos = null;
    try {/*  w  w  w  . java2 s  .  c  om*/
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        jos = new JarOutputStream(bos);
        if (zipMap != null) {
            Set<Entry<String, byte[]>> entrySet = zipMap.entrySet();
            for (Entry<String, byte[]> entry : entrySet) {
                String name = entry.getKey();
                byte[] x_bytes = entry.getValue();
                inputStream = new ByteArrayInputStream(x_bytes);
                if (name != null && inputStream != null) {
                    bis = new BufferedInputStream(inputStream);
                    JarEntry jarEntry = new JarEntry(name);
                    jos.putNextEntry(jarEntry);
                    while ((len = bis.read(buf)) >= 0) {
                        jos.write(buf, 0, len);
                    }
                    IOUtils.closeStream(bis);
                    jos.closeEntry();
                }
                IOUtils.closeStream(inputStream);
            }
        }
        jos.flush();
        jos.close();

        bytes = baos.toByteArray();
        IOUtils.closeStream(baos);
        IOUtils.closeStream(bos);
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(inputStream);
        IOUtils.closeStream(baos);
        IOUtils.closeStream(bos);
    }
}

From source file:org.jahia.modules.serversettings.portlets.BasePortletHelper.java

void copyEntries(JarInputStream source, JarOutputStream dest) throws IOException {
    JarEntry originalJarEntry = source.getNextJarEntry();
    while (originalJarEntry != null) {
        final JarEntry newJarEntry = cloneEntry(originalJarEntry);
        dest.putNextEntry(newJarEntry);
        if (!handled(originalJarEntry, source, dest)) {
            IOUtils.copy(source, dest);//from ww  w.jav  a2s  . c om
        }
        dest.closeEntry();
        dest.flush();
        originalJarEntry = source.getNextJarEntry();
    }
}

From source file:org.apache.crunch.WordCountHBaseTest.java

private void jarUp(JarOutputStream jos, File baseDir, String classDir) throws IOException {
    File file = new File(baseDir, classDir);
    JarEntry e = new JarEntry(classDir);
    e.setTime(file.lastModified());/*from w ww . j a  va2  s .com*/
    jos.putNextEntry(e);
    ByteStreams.copy(new FileInputStream(file), jos);
    jos.closeEntry();
}

From source file:Main.java

void createJarArchive(File archiveFile, File[] tobeJared) throws Exception {
    byte buffer[] = new byte[BUFFER_SIZE];
    FileOutputStream stream = new FileOutputStream(archiveFile);
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    for (int i = 0; i < tobeJared.length; i++) {
        if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
            continue; // Just in case...
        JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);
        FileInputStream in = new FileInputStream(tobeJared[i]);
        while (true) {
            int nRead = in.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            out.write(buffer, 0, nRead);
        }/*from  w  w w.j a v a2 s  .  c  o  m*/
        in.close();
    }
    out.close();
    stream.close();
}

From source file:com.h3xstream.findbugs.test.service.FindBugsLauncher.java

private void addFilesToStream(final ClassLoader cl, final JarOutputStream jar, final File dir,
        final String path) throws IOException {
    for (final File nextFile : dir.listFiles()) {
        if (nextFile.isFile()) {
            final String resource = path + nextFile.getName();
            jar.putNextEntry(new ZipEntry(resource));
            jar.write(IOUtils.toByteArray(cl.getResourceAsStream("metadata/" + resource)));
        } else {//from   w ww  .  j  a  v  a 2  s  .co  m
            addFilesToStream(cl, jar, nextFile, path + nextFile.getName() + "/");
        }
    }
}

From source file:com.hellblazer.process.JavaProcessTest.java

protected void copyTestJarFile() throws Exception {
    String classFileName = HelloWorld.class.getCanonicalName().replace('.', '/') + ".class";
    URL classFile = getClass().getResource("/" + classFileName);
    assertNotNull(classFile);//from w ww  .  ja  v  a 2s.  c  o m

    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.putValue("Manifest-Version", "1.0");
    attributes.putValue("Main-Class", HelloWorld.class.getCanonicalName());

    FileOutputStream fos = new FileOutputStream(new File(testDir, TEST_JAR));
    JarOutputStream jar = new JarOutputStream(fos, manifest);
    JarEntry entry = new JarEntry(classFileName);
    jar.putNextEntry(entry);
    InputStream in = classFile.openStream();
    byte[] buffer = new byte[1024];
    for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
        jar.write(buffer, 0, read);
    }
    in.close();
    jar.closeEntry();
    jar.close();
}

From source file:org.apache.pluto.util.assemble.io.JarStreamingAssembly.java

/**
 * Reads the source JarInputStream, copying entries to the destination JarOutputStream. 
 * The web.xml and portlet.xml are cached, and after the entire archive is copied 
 * (minus the web.xml) a re-written web.xml is generated and written to the 
 * destination JAR.//ww w.j  a va  2s.c  o m
 * 
 * @param source the WAR source input stream
 * @param dest the WAR destination output stream
 * @param dispatchServletClass the name of the dispatch class
 * @throws IOException
 */
public static void assembleStream(JarInputStream source, JarOutputStream dest, String dispatchServletClass)
        throws IOException {

    try {
        //Need to buffer the web.xml and portlet.xml files for the rewritting
        JarEntry servletXmlEntry = null;
        byte[] servletXmlBuffer = null;
        byte[] portletXmlBuffer = null;

        JarEntry originalJarEntry;

        //Read the source archive entry by entry
        while ((originalJarEntry = source.getNextJarEntry()) != null) {

            final JarEntry newJarEntry = smartClone(originalJarEntry);
            originalJarEntry = null;

            //Capture the web.xml JarEntry and contents as a byte[], don't write it out now or
            //update the CRC or length of the destEntry.
            if (Assembler.SERVLET_XML.equals(newJarEntry.getName())) {
                servletXmlEntry = newJarEntry;
                servletXmlBuffer = IOUtils.toByteArray(source);
            }

            //Capture the portlet.xml contents as a byte[]
            else if (Assembler.PORTLET_XML.equals(newJarEntry.getName())) {
                portletXmlBuffer = IOUtils.toByteArray(source);
                dest.putNextEntry(newJarEntry);
                IOUtils.write(portletXmlBuffer, dest);
            }

            //Copy all other entries directly to the output archive
            else {
                dest.putNextEntry(newJarEntry);
                IOUtils.copy(source, dest);
            }

            dest.closeEntry();
            dest.flush();

        }

        // If no portlet.xml was found in the archive, skip the assembly step.
        if (portletXmlBuffer != null) {
            // container for assembled web.xml bytes
            final byte[] webXmlBytes;

            // Checks to make sure the web.xml was found in the archive
            if (servletXmlBuffer == null) {
                throw new FileNotFoundException(
                        "File '" + Assembler.SERVLET_XML + "' could not be found in the source input stream.");
            }

            //Create streams of the byte[] data for the updater method
            final InputStream webXmlIn = new ByteArrayInputStream(servletXmlBuffer);
            final InputStream portletXmlIn = new ByteArrayInputStream(portletXmlBuffer);
            final ByteArrayOutputStream webXmlOut = new ByteArrayOutputStream(servletXmlBuffer.length);

            //Update the web.xml
            WebXmlStreamingAssembly.assembleStream(webXmlIn, portletXmlIn, webXmlOut, dispatchServletClass);
            IOUtils.copy(webXmlIn, webXmlOut);
            webXmlBytes = webXmlOut.toByteArray();

            //If no compression is being used (STORED) we have to manually update the size and crc
            if (servletXmlEntry.getMethod() == ZipEntry.STORED) {
                servletXmlEntry.setSize(webXmlBytes.length);
                final CRC32 webXmlCrc = new CRC32();
                webXmlCrc.update(webXmlBytes);
                servletXmlEntry.setCrc(webXmlCrc.getValue());
            }

            //write out the assembled web.xml entry and contents
            dest.putNextEntry(servletXmlEntry);
            IOUtils.write(webXmlBytes, dest);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Jar stream " + source + " successfully assembled.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No portlet XML file was found, assembly was not required.");
            }

            //copy the original, unmodified web.xml entry to the destination
            dest.putNextEntry(servletXmlEntry);
            IOUtils.write(servletXmlBuffer, dest);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Jar stream " + source + " successfully assembled.");
            }
        }

    } finally {

        dest.flush();
        dest.close();

    }
}