Example usage for org.apache.commons.compress.archivers.jar JarArchiveEntry setTime

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

Introduction

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

Prototype

public void setTime(long time) 

Source Link

Document

Sets the last modification time of the 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;//from  w  ww .  j  a v  a2s  .  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: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 ww. j  a va  2 s  . c  o  m
 *
 * @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   www  .j  av a2 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:org.moe.cli.utils.ArchiveUtils.java

public static void addFileToJar(File baseDir, File source, JarArchiveOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {//from   w ww .  j a v a 2  s.co  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();
    }
}

From source file:org.springframework.boot.loader.tools.JarWriter.java

/**
 * Write a nested library./*from  ww w.ja  v  a  2  s .c o  m*/
 * @param destination the destination of the library
 * @param library the library
 * @throws IOException if the write fails
 */
public void writeNestedLibrary(String destination, Library library) throws IOException {
    File file = library.getFile();
    JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName());
    entry.setTime(getNestedLibraryTime(file));
    new CrcAndSize(file).setupStoredEntry(entry);
    writeEntry(entry, new InputStreamEntryWriter(new FileInputStream(file), true),
            new LibraryUnpackHandler(library));
}