Example usage for java.util.jar JarOutputStream write

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

Introduction

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

Prototype

public synchronized void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes an array of bytes to the current ZIP entry data.

Usage

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

public static byte[] getZipBytes(Map<String, InputStream> dataMap) {
    byte[] bytes = null;
    try {/*from ww w. jav a2 s  .co  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        JarOutputStream jos = new JarOutputStream(bos);
        if (dataMap != null) {
            Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
            for (Entry<String, InputStream> entry : entrySet) {
                String name = entry.getKey();
                InputStream inputStream = entry.getValue();
                if (name != null && inputStream != null) {
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    JarEntry jarEntry = new JarEntry(name);
                    jos.putNextEntry(jarEntry);

                    while ((len = bis.read(buf)) >= 0) {
                        jos.write(buf, 0, len);
                    }

                    bis.close();
                    jos.closeEntry();
                }
            }
        }
        jos.flush();
        jos.close();
        bos.flush();
        bos.close();
        bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:JarMaker.java

/**
 * Combine several jar files and some given files into one jar file.
 * @param outJar The output jar file's filename.
 * @param inJarsList The jar files to be combined
 * @param filter A filter that exclude some entries of input jars. User
 *        should implement the EntryFilter interface.
 * @param inFileList The files to be added into the jar file.
 * @param prefixs The prefixs of files to be added into the jar file.
 *        inFileList and prefixs should be paired.
 * @throws FileNotFoundException// w  ww . java2s .  co  m
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList,
        String[] prefixs) throws FileNotFoundException, IOException {

    JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar));
    ArrayList entryList = new ArrayList();
    for (int i = 0; i < inJarsList.length; i++) {
        BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i]));
        ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
        byte abyte0[] = new byte[1024 * 4];
        for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
                .getNextEntry()) {
            if (filter.accept(zipentry)) {
                if (!entryList.contains(zipentry.getName())) {
                    jout.putNextEntry(zipentry);
                    if (!zipentry.isDirectory()) {
                        int j;
                        try {
                            while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                                jout.write(abyte0, 0, j);
                        } catch (IOException ie) {
                            throw ie;
                        }
                    }
                    entryList.add(zipentry.getName());
                }
            }
        }
        zipinputstream.close();
        bufferedinputstream.close();
    }
    for (int i = 0; i < inFileList.length; i++) {
        add(jout, new File(inFileList[i]), prefixs[i]);
    }
    jout.close();
}

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

private static void writeManifest(byte[] manifest, JarOutputStream jos) throws IOException {

    // Manifest file entry
    JarEntry mfJarEntry = new JarEntry(MANIFEST_LOCATION);
    jos.putNextEntry(mfJarEntry);/*from   ww  w .  j a  va2s  . co  m*/

    // Write content
    ByteArrayInputStream bais = null;

    try {
        bais = new ByteArrayInputStream(manifest);

        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: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  ww w  .j  a v  a2s. co m

    // 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:ezbake.frack.submitter.util.JarUtil.java

private static void add(File source, final String prefix, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    log.debug("Adding file {} to jar", source.getName());
    try {//from  w  w  w .j ava2s.  co m
        String entryPath = source.getPath().replace("\\", "/").replace(prefix, "");
        if (entryPath.startsWith("/")) {
            entryPath = entryPath.substring(1);
        }
        if (source.isDirectory()) {
            if (!entryPath.isEmpty()) {
                if (!entryPath.endsWith("/")) {
                    entryPath += "/";
                }
                JarEntry entry = new JarEntry(entryPath);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles()) {
                add(nestedFile, prefix, target);
            }
        } else {
            JarEntry entry = new JarEntry(entryPath);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            target.closeEntry();
        }
    } finally {
        if (in != null) {
            in.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 ww w  . ja v a 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: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  . j a  v  a 2  s.  com*/
        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:gov.nih.nci.restgen.util.GeneratorUtil.java

private static void add(File source, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {/*from  ww w  . ja v  a  2  s . co m*/
        if (source.isDirectory()) {
            String name = source.getPath().replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/"))
                    name += "/";
                JarEntry entry = new JarEntry(name);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles())
                add(nestedFile, target);
            return;
        }

        JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[1024];
        while (true) {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:JarUtils.java

/**
 * This recursive method writes all matching files and directories to
 * the jar output stream.// www. j a va 2  s  .c o  m
 */
private static void jar(File src, String prefix, JarInfo info) throws IOException {

    JarOutputStream jout = info.out;
    if (src.isDirectory()) {
        // create / init the zip entry
        prefix = prefix + src.getName() + "/";
        ZipEntry entry = new ZipEntry(prefix);
        entry.setTime(src.lastModified());
        entry.setMethod(JarOutputStream.STORED);
        entry.setSize(0L);
        entry.setCrc(0L);
        jout.putNextEntry(entry);
        jout.closeEntry();

        // process the sub-directories
        File[] files = src.listFiles(info.filter);
        for (int i = 0; i < files.length; i++) {
            jar(files[i], prefix, info);
        }
    } else if (src.isFile()) {
        // get the required info objects
        byte[] buffer = info.buffer;

        // create / init the zip entry
        ZipEntry entry = new ZipEntry(prefix + src.getName());
        entry.setTime(src.lastModified());
        jout.putNextEntry(entry);

        // dump the file
        FileInputStream in = new FileInputStream(src);
        int len;
        while ((len = in.read(buffer, 0, buffer.length)) != -1) {
            jout.write(buffer, 0, len);
        }
        in.close();
        jout.closeEntry();
    }
}

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);
        }/*  ww  w.ja  v a 2 s  .  c om*/
    } 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();
    }
}