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

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

Introduction

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

Prototype

public void putArchiveEntry(ArchiveEntry ze) throws IOException 

Source Link

Usage

From source file:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java

private static void writeJarEntry(JarArchiveOutputStream jaos, String name, byte[] data) throws IOException {
    ZipArchiveEntry entry = new JarArchiveEntry(name);
    entry.setSize(data.length);/*  www  .  j av a  2  s  .c o m*/
    jaos.putArchiveEntry(entry);
    jaos.write(data);
    jaos.closeArchiveEntry();
}

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Recursively create JAR archive from directory helper utility 
 *//*w  ww  .  j  av a  2s.c o m*/
private static void createJarHelper(File fs, JarArchiveOutputStream jaos, String zePath) throws IOException {
    log.debug("createJarHelper({}, {}, {})", new Object[] { fs, jaos, zePath });
    File[] files = fs.listFiles();

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            log.debug("DIRECTORY {}", files[i]);
            JarArchiveEntry jae = new JarArchiveEntry(zePath + "/" + files[i].getName() + "/");
            jaos.putArchiveEntry(jae);
            jaos.closeArchiveEntry();

            createJarHelper(files[i], jaos, zePath + "/" + files[i].getName());
        } else {
            log.debug("FILE {}", files[i]);
            JarArchiveEntry jae = new JarArchiveEntry(zePath + "/" + files[i].getName());
            jaos.putArchiveEntry(jae);
            FileInputStream fis = new FileInputStream(files[i]);
            IOUtils.copy(fis, jaos);
            fis.close();
            jaos.closeArchiveEntry();
        }
    }

    log.debug("createJarHelper: void");
}

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Recursively create JAR archive from directory 
 *///from  w w  w.  j  a  va2s .c  o  m
public static void createJar(File path, String root, OutputStream os) throws IOException {
    log.debug("createJar({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        JarArchiveOutputStream jaos = new JarArchiveOutputStream(os);
        jaos.setComment("Generated by openkm");
        jaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        jaos.setUseLanguageEncodingFlag(true);
        jaos.setFallbackToUTF8(true);
        jaos.setEncoding("UTF-8");

        // Prevents java.util.jar.JarException: JAR file must have at least one entry
        JarArchiveEntry jae = new JarArchiveEntry(root + "/");
        jaos.putArchiveEntry(jae);
        jaos.closeArchiveEntry();

        createJarHelper(path, jaos, root);

        jaos.flush();
        jaos.finish();
        jaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createJar: void");
}

From source file:com.openkm.util.ArchiveUtils.java

/**
 * Recursively create JAR archive from directory
 *///from   w  w w. j a  v a 2s .  c  o  m
public static void createJar(File path, String root, OutputStream os) throws IOException {
    log.debug("createJar({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        JarArchiveOutputStream jaos = new JarArchiveOutputStream(os);
        jaos.setComment("Generated by OpenKM");
        jaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        jaos.setUseLanguageEncodingFlag(true);
        jaos.setFallbackToUTF8(true);
        jaos.setEncoding("UTF-8");

        // Prevents java.util.jar.JarException: JAR file must have at least one entry
        JarArchiveEntry jae = new JarArchiveEntry(root + "/");
        jaos.putArchiveEntry(jae);
        jaos.closeArchiveEntry();

        createJarHelper(path, jaos, root);

        jaos.flush();
        jaos.finish();
        jaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createJar: void");
}

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;//from  w  w  w. j  a v a  2 s  .c om

    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:com.jrummyapps.busybox.utils.ZipSigner.java

/**
 * Tool to sign JAR files (including APKs and OTA updates) in a way compatible with the mincrypt verifier, using
 * SHA1 and RSA keys.// www.  ja v  a2s  .  c  om
 *
 * @param unsignedZip
 *     The path to the APK, ZIP, JAR to sign
 * @param destination
 *     The output file
 * @return true if successfully signed the file
 */
public static boolean signZip(File unsignedZip, File destination) {
    final AssetManager am = App.getContext().getAssets();
    JarArchiveOutputStream outputJar = null;
    JarFile inputJar = null;

    try {
        X509Certificate publicKey = readPublicKey(am.open(PUBLIC_KEY));
        PrivateKey privateKey = readPrivateKey(am.open(PRIVATE_KEY));

        // Assume the certificate is valid for at least an hour.
        long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;

        inputJar = new JarFile(unsignedZip, false); // Don't verify.
        FileOutputStream stream = new FileOutputStream(destination);
        outputJar = new JarArchiveOutputStream(stream);
        outputJar.setLevel(9);

        // MANIFEST.MF
        Manifest manifest = addDigestsToManifest(inputJar);
        JarArchiveEntry je = new JarArchiveEntry(JarFile.MANIFEST_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        manifest.write(outputJar);

        ZipSignature signature1 = new ZipSignature();
        signature1.initSign(privateKey);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writeSignatureFile(manifest, out);

        // CERT.SF
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        je = new JarArchiveEntry(CERT_SF_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        byte[] sfBytes = writeSignatureFile(manifest, new SignatureOutputStream(outputJar, signature));

        signature1.update(sfBytes);
        byte[] signatureBytes = signature1.sign();

        // CERT.RSA
        je = new JarArchiveEntry(CERT_RSA_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);

        outputJar.write(readContentAsBytes(am.open(TEST_KEY)));
        outputJar.write(signatureBytes);

        copyFiles(manifest, inputJar, outputJar, timestamp);
    } catch (Exception e) {
        Crashlytics.logException(e);
        return false;
    } finally {
        IoUtils.closeQuietly(inputJar);
        IoUtils.closeQuietly(outputJar);
    }
    return true;
}

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

/**
 * Tool to sign JAR files (including APKs and OTA updates) in a way compatible with the mincrypt verifier, using
 * SHA1 and RSA keys.//from   ww  w . j  a  va 2s.  com
 *
 * @param unsignedZip
 *     The path to the APK, ZIP, JAR to sign
 * @param destination
 *     The output file
 * @return true if successfully signed the file
 */
public static boolean signZip(File unsignedZip, File destination) {
    final AssetManager am = App.getContext().getAssets();
    JarArchiveOutputStream outputJar = null;
    JarFile inputJar = null;

    try {
        X509Certificate publicKey = readPublicKey(am.open(PUBLIC_KEY));
        PrivateKey privateKey = readPrivateKey(am.open(PRIVATE_KEY));

        // Assume the certificate is valid for at least an hour.
        long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;

        inputJar = new JarFile(unsignedZip, false); // Don't verify.
        FileOutputStream stream = new FileOutputStream(destination);
        outputJar = new JarArchiveOutputStream(stream);
        outputJar.setLevel(9);

        // MANIFEST.MF
        Manifest manifest = addDigestsToManifest(inputJar);
        JarArchiveEntry je = new JarArchiveEntry(JarFile.MANIFEST_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        manifest.write(outputJar);

        ZipSignature signature1 = new ZipSignature();
        signature1.initSign(privateKey);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writeSignatureFile(manifest, out);

        // CERT.SF
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        je = new JarArchiveEntry(CERT_SF_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);
        byte[] sfBytes = writeSignatureFile(manifest, new SignatureOutputStream(outputJar, signature));

        signature1.update(sfBytes);
        byte[] signatureBytes = signature1.sign();

        // CERT.RSA
        je = new JarArchiveEntry(CERT_RSA_NAME);
        je.setTime(timestamp);
        outputJar.putArchiveEntry(je);

        outputJar.write(readContentAsBytes(am.open(TEST_KEY)));
        outputJar.write(signatureBytes);

        copyFiles(manifest, inputJar, outputJar, timestamp);
    } catch (Exception e) {
        Crashlytics.logException(e);
        return false;
    } finally {
        IOUtils.closeQuietly(inputJar);
        IOUtils.closeQuietly(outputJar);
    }
    return true;
}

From source file:org.apache.hadoop.hive.ql.processors.CompileProcessor.java

@VisibleForTesting
/**/*from w  ww .j  a v a 2  s .c  om*/
 * Method converts statement into a file, compiles the file and then packages the file.
 * @param ss
 * @return Response code of 0 for success 1 for failure
 * @throws CompileProcessorException
 */
CommandProcessorResponse compile(SessionState ss) throws CompileProcessorException {
    Project proj = new Project();
    String ioTempDir = System.getProperty(IO_TMP_DIR);
    File ioTempFile = new File(ioTempDir);
    if (!ioTempFile.exists()) {
        throw new CompileProcessorException(ioTempDir + " does not exists");
    }
    if (!ioTempFile.isDirectory() || !ioTempFile.canWrite()) {
        throw new CompileProcessorException(ioTempDir + " is not a writable directory");
    }
    Groovyc g = new Groovyc();
    long runStamp = System.currentTimeMillis();
    String jarId = myId + "_" + runStamp;
    g.setProject(proj);
    Path sourcePath = new Path(proj);
    File destination = new File(ioTempFile, jarId + "out");
    g.setDestdir(destination);
    File input = new File(ioTempFile, jarId + "in");
    sourcePath.setLocation(input);
    g.setSrcdir(sourcePath);
    input.mkdir();

    File fileToWrite = new File(input, this.named);
    try {
        Files.write(this.code, fileToWrite, Charset.forName("UTF-8"));
    } catch (IOException e1) {
        throw new CompileProcessorException("writing file", e1);
    }
    destination.mkdir();
    try {
        g.execute();
    } catch (BuildException ex) {
        throw new CompileProcessorException("Problem compiling", ex);
    }
    File testArchive = new File(ioTempFile, jarId + ".jar");
    JarArchiveOutputStream out = null;
    try {
        out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
        for (File f : destination.listFiles()) {
            JarArchiveEntry jentry = new JarArchiveEntry(f.getName());
            FileInputStream fis = new FileInputStream(f);
            out.putArchiveEntry(jentry);
            IOUtils.copy(fis, out);
            fis.close();
            out.closeArchiveEntry();
        }
        out.finish();
    } catch (IOException e) {
        throw new CompileProcessorException("Exception while writing jar", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException WhatCanYouDo) {
            }
        }
    }

    if (ss != null) {
        ss.add_resource(ResourceType.JAR, testArchive.getAbsolutePath());
    }
    CommandProcessorResponse good = new CommandProcessorResponse(0, testArchive.getAbsolutePath(), null);
    return good;
}

From source file:org.apache.openejb.maven.plugin.customizer.monkey.jar.JarPatcher.java

private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix)
        throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final ZipArchiveEntry zip = new ZipArchiveEntry(f, path);
    zip.setMethod(method);//  ww  w . j a  v a 2s .c  o m
    final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip);
    jar.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        jar.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                jar(method, jar, child.getCanonicalFile(), prefix);
            }
        }
    } else {
        final InputStream is = new FileInputStream(f);
        IOUtils.copy(is, jar);
        is.close();
        jar.closeArchiveEntry();
    }
}

From source file:org.apache.openejb.maven.plugin.customizer.monkey.MonkeyTest.java

private File prepareProject() throws IOException {
    final File target = new File("target/MonkeyTest_run" + System.currentTimeMillis() + "/mvn/target");
    target.mkdirs();/* w  w w. jav  a2  s .c o m*/

    final File classes = new File(target, "classes");
    classes.mkdirs();

    writeBinary(classes, "target/test-classes/test/patch/MyMain.class", "test/patch/MyMain.class");
    writeBinary(classes, "target/test-classes/test/patch/foo/Another.class", "test/patch/foo/Another.class");

    final File tomee = new File(target, "tomee");
    final File lib = new File(tomee, "lib");
    lib.mkdirs();

    // create the jar to patch, it is invalid but when patched it should work
    JarArchiveOutputStream stream = null;
    try {
        stream = new JarArchiveOutputStream(new FileOutputStream(new File(lib, "t.jar")));
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/MyMain.class"));
        stream.write("invalid".getBytes());
        stream.closeArchiveEntry();
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/foo/Another.class"));
        stream.write("invalid-too".getBytes());
        stream.closeArchiveEntry();
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }

    return tomee;
}