Example usage for org.apache.commons.compress.archivers.jar JarArchiveOutputStream write

List of usage examples for org.apache.commons.compress.archivers.jar JarArchiveOutputStream write

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.jar JarArchiveOutputStream write.

Prototype

public void write(byte[] b, int offset, int length) throws IOException 

Source Link

Document

Writes bytes to ZIP entry.

Usage

From source file:com.jrummyapps.busybox.signing.ZipSigner.java

/** Copy all the files in a manifest from input to output. */
private static void copyFiles(Manifest manifest, JarFile in, JarArchiveOutputStream out, long timestamp)
        throws IOException {
    final byte[] buffer = new byte[4096];
    int num;//  ww w .j ava  2 s  .  c  o  m

    final Map<String, Attributes> entries = manifest.getEntries();
    final List<String> names = new ArrayList<>(entries.keySet());
    Collections.sort(names);
    for (final String name : names) {
        final JarEntry inEntry = in.getJarEntry(name);
        if (inEntry.getMethod() == JarArchiveEntry.STORED) {
            // Preserve the STORED method of the input entry.
            out.putArchiveEntry(new JarArchiveEntry(inEntry));
        } else {
            // Create a new entry so that the compressed len is recomputed.
            final JarArchiveEntry je = new JarArchiveEntry(name);
            je.setTime(timestamp);
            out.putArchiveEntry(je);
        }

        final InputStream data = in.getInputStream(inEntry);
        while ((num = data.read(buffer)) > 0) {
            out.write(buffer, 0, num);
        }
        out.flush();
        out.closeArchiveEntry();
    }
}

From source file:org.moe.cli.utils.ArchiveUtils.java

public static void addFileToJar(File baseDir, File source, JarArchiveOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {/*  ww w. j a  v  a2  s .  c  o m*/
        String baseName = baseDir.getPath().replace("\\", "/");
        baseName = baseName.endsWith("/") ? baseName : baseName + "/";
        String name = source.getPath().replace("\\", "/").replace(baseName, "");
        if (source.isDirectory()) {

            if (!name.isEmpty()) {
                if (!name.endsWith("/"))
                    name += "/";
                JarArchiveEntry entry = new JarArchiveEntry(name);
                entry.setTime(source.lastModified());
                target.putArchiveEntry(entry);
                target.closeArchiveEntry();
            }
            for (File nestedFile : source.listFiles())
                addFileToJar(baseDir, nestedFile, target);
            return;
        }

        JarArchiveEntry entry = new JarArchiveEntry(name);
        entry.setTime(source.lastModified());
        target.putArchiveEntry(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.closeArchiveEntry();
    } finally {
        if (in != null)
            in.close();
    }
}