Example usage for java.util.jar JarOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

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

private static void writeSignatureFile(byte[] sf, String signatureName, JarOutputStream jos)
        throws IOException {

    // Signature file entry
    JarEntry sfJarEntry = new JarEntry(
            MessageFormat.format(METAINF_FILE_LOCATION, signatureName, SIGNATURE_EXT).toUpperCase());
    jos.putNextEntry(sfJarEntry);/*from  w w  w  .j a  v  a 2  s  . com*/

    // Write content
    ByteArrayInputStream bais = null;

    try {
        bais = new ByteArrayInputStream(sf);

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

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

        jos.closeEntry();
    } finally {
        IOUtils.closeQuietly(bais);
    }
}

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  w  w  . j  av  a  2 s.  co m
    jos.putNextEntry(e);
    ByteStreams.copy(new FileInputStream(file), jos);
    jos.closeEntry();
}

From source file:org.sakaiproject.kernel.component.core.test.ComponentLoaderServiceImplTest.java

/**
 * @param file/*from w  ww .  ja v  a2 s  . co m*/
 * @throws IOException
 */
private void createComponent(File f, String component) throws IOException {
    if (f.getParentFile().mkdirs()) {
        if (debug)
            LOG.debug("Created Directory " + f);
    }
    JarOutputStream jarOutput = new JarOutputStream(new FileOutputStream(f));
    JarEntry jarEntry = new JarEntry("SAKAI-INF/component.xml");
    jarOutput.putNextEntry(jarEntry);
    String componentXml = ResourceLoader.readResource(component, this.getClass().getClassLoader());
    jarOutput.write(componentXml.getBytes("UTF-8"));
    jarOutput.closeEntry();
    jarOutput.close();
}

From source file:com.enderville.enderinstaller.util.InstallScript.java

/**
 * Repackages all the files in the tmp directory to the new minecraft.jar
 *
 * @param tmp The temp directory where mods were installed.
 * @param mcjar The location to save the new minecraft.jar.
 * @throws IOException/*from   w  w w  .j a  va 2 s  .  co  m*/
 */
public static void repackMCJar(File tmp, File mcjar) throws IOException {
    byte[] dat = new byte[4 * 1024];

    JarOutputStream jarout = new JarOutputStream(FileUtils.openOutputStream(mcjar));

    Queue<File> queue = new LinkedList<File>();
    for (File f : tmp.listFiles()) {
        queue.add(f);
    }

    while (!queue.isEmpty()) {
        File f = queue.poll();
        if (f.isDirectory()) {
            for (File child : f.listFiles()) {
                queue.add(child);
            }
        } else {
            //TODO need a better way to do this
            String name = f.getPath().substring(tmp.getPath().length() + 1);
            //TODO is this formatting really required for jars?
            name = name.replace("\\", "/");
            if (f.isDirectory() && !name.endsWith("/")) {
                name = name + "/";
            }
            JarEntry entry = new JarEntry(name);
            jarout.putNextEntry(entry);

            FileInputStream in = new FileInputStream(f);
            int len = -1;
            while ((len = in.read(dat)) > 0) {
                jarout.write(dat, 0, len);
            }
            in.close();
        }
        jarout.closeEntry();
    }
    jarout.close();

}

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);//from  w w  w  . j a v a  2s  .  co m
        if (!handled(originalJarEntry, source, dest)) {
            IOUtils.copy(source, dest);
        }
        dest.closeEntry();
        dest.flush();
        originalJarEntry = source.getNextJarEntry();
    }
}

From source file:com.streamsets.pipeline.maven.rbgen.RBGenMojo.java

private void addFile(String path, File file, JarOutputStream jar) throws IOException {
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
        JarEntry entry = new JarEntry(path);
        entry.setTime(file.lastModified());
        jar.putNextEntry(entry);// ww w.j  a v  a2 s.  co m
        IOUtils.copy(in, jar);
        jar.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 {//ww  w  . j  a  v  a 2s .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:com.glaf.core.util.ZipUtils.java

private static void recurseFiles(JarOutputStream jos, File file, String s)
        throws IOException, FileNotFoundException {

    if (file.isDirectory()) {
        s = s + file.getName() + "/";
        jos.putNextEntry(new JarEntry(s));
        String as[] = file.list();
        if (as != null) {
            for (int i = 0; i < as.length; i++)
                recurseFiles(jos, new File(file, as[i]), s);
        }/*from   w w w. j  a  v  a2  s  .  c  o  m*/
    } else {
        if (file.getName().endsWith("MANIFEST.MF") || file.getName().endsWith("META-INF/MANIFEST.MF")) {
            return;
        }
        JarEntry jarentry = new JarEntry(s + file.getName());
        FileInputStream fileinputstream = new FileInputStream(file);
        BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
        jos.putNextEntry(jarentry);
        while ((len = bufferedinputstream.read(buf)) >= 0) {
            jos.write(buf, 0, len);
        }
        bufferedinputstream.close();
        jos.closeEntry();
    }
}

From source file:com.github.sampov2.OneJarMojo.java

private void addToZip(JarOutputStream out, ZipEntry entry, InputStream in) throws IOException {
    try {//  w ww .java2  s.  c  o  m
        out.putNextEntry(entry);
        IOUtils.copy(in, out);
        out.closeEntry();
    } catch (ZipException e) {
        if (e.getMessage().startsWith("duplicate entry")) {
            // A Jar with the same name was already added. Let's add this one using a modified name:
            final ZipEntry alternativeEntry = new ZipEntry(entry.getName() + "-DUPLICATE-FILENAME-"
                    + alternativeEntryCounter.incrementAndGet() + ".jar");
            addToZip(out, alternativeEntry, in);
        } else {
            throw e;
        }
    }
}

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  .  j  a va  2 s . c om*/

    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();
}