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

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

Introduction

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

Prototype

public JarArchiveOutputStream(final OutputStream out) 

Source Link

Usage

From source file:es.jamisoft.comun.utils.compression.Jar.java

public void comprimir(String asDir) {
    try {/* ww  w  . ja  v  a 2 s . co m*/
        isDirectorioRelativoComp = asDir;
        sout = new JarArchiveOutputStream(new FileOutputStream(isFicheroJar));
        sout.setLevel(6);
        compress(asDir);
        metaInf();
        sout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * Recursively create JAR archive from directory 
 *//*from   w  w w.ja v  a  2  s.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 av a  2 s .co  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.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./*from   w  w w  . ja va  2 s .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: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 w  ww  .j a  v a  2  s.  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:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java

/**
 * Creates a JAR as a temporary file. Simple manifest file will automatically be inserted.
 *
 * @param entries files to put inside of the jar
 * @return resulting jar file//from   w  w w  .  j a  va 2s . c o  m
 * @throws IOException if any IO errors occur
 * @throws NullPointerException if any argument is {@code null} or contains {@code null} elements
 */
public static File createJar(JarEntry... entries) throws IOException {
    Validate.notNull(entries);
    Validate.noNullElements(entries);

    File tempFile = File.createTempFile(TestUtils.class.getSimpleName(), ".jar");
    tempFile.deleteOnExit();

    try (FileOutputStream fos = new FileOutputStream(tempFile);
            JarArchiveOutputStream jaos = new JarArchiveOutputStream(fos)) {
        writeJarEntry(jaos, MANIFEST_PATH, MANIFEST_TEXT.getBytes(Charsets.UTF_8));
        for (JarEntry entry : entries) {
            writeJarEntry(jaos, entry.name, entry.data);
        }
    }

    return tempFile;
}

From source file:org.anarres.gradle.plugin.jarjar.JarjarCopyAction.java

@Nonnull
@Override/*from  ww w.  ja va2  s.  c om*/
public WorkResult execute(@Nonnull final CopyActionProcessingStream stream) {
    LOG.info("CopyAction Executing  " + stream);

    stream.process(new ScanAction());

    final JarArchiveOutputStream zipOutStr;

    try {
        zipOutStr = new JarArchiveOutputStream(new FileOutputStream(zipFile));
    } catch (Exception e) {
        throw new GradleException(String.format("Could not create ZIP '%s'.", zipFile), e);
    }

    try {
        IoActions.withResource(zipOutStr, new Action<JarArchiveOutputStream>() {
            @Override
            public void execute(@Nonnull JarArchiveOutputStream outputStream) {
                stream.process(new ProcessAction(outputStream));
            }
        });
    } catch (UncheckedIOException e) {
        if (e.getCause() instanceof Zip64RequiredException) {
            throw new org.gradle.api.tasks.bundling.internal.Zip64RequiredException(String.format(
                    "%s\n\nTo build this archive, please enable the zip64 extension.\nSee: %s",
                    e.getCause().getMessage(), documentationRegistry.getDslRefForProperty(Zip.class, "zip64")));
        }
    }

    return new SimpleWorkResult(true);
}

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

@VisibleForTesting
/**//from w w w.  ja va  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 File exploded, final File target) {
    JarArchiveOutputStream stream = null;
    try {//from  w  w w  . ja  va  2  s  .  co  m
        stream = new JarArchiveOutputStream(new FileOutputStream(target));
        final String prefix = exploded.getCanonicalFile().getAbsolutePath() + File.separator;
        for (final String f : exploded.list()) {
            jar(method, stream, new File(exploded, f).getCanonicalFile(), prefix);
        }
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }
}

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();/*from  www  .  jav a2s  . co  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;
}