Example usage for java.util.jar JarEntry JarEntry

List of usage examples for java.util.jar JarEntry JarEntry

Introduction

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

Prototype

public JarEntry(JarEntry je) 

Source Link

Document

Creates a new JarEntry with fields taken from the specified JarEntry object.

Usage

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 w w  w  .  j  ava 2 s.c om*/

    // 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);//ww w.j  ava 2 s  . c om

    // 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:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * jarclass//from  w  w w  . j av a2 s.c o m
 * @param inJar
 * @param removeClasses
 */
public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException {
    if (null == removeClasses || removeClasses.isEmpty()) {
        return;
    }
    File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp");
    File outParentFolder = outJar.getParentFile();
    if (!outParentFolder.exists()) {
        outParentFolder.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(outJar);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inJar);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
                continue;
            }
            String name = entry.getName();
            String className = getClassName(name);
            if (removeClasses.contains(className)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
    FileUtils.deleteQuietly(inJar);
    FileUtils.moveFile(outJar, inJar);
}

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

private static void writeSignatureBlock(byte[] sigBlock, SignatureType signatureType, String signatureName,
        JarOutputStream jos) throws IOException {

    // Block's extension depends on signature type
    String extension = null;/* ww  w .  j av a  2s .co  m*/

    if (signatureType == SHA1_DSA) {
        extension = DSA_SIG_BLOCK_EXT;
    } else {
        extension = RSA_SIG_BLOCK_EXT;
    }

    // Signature block entry
    JarEntry bkJarEntry = new JarEntry(
            MessageFormat.format(METAINF_FILE_LOCATION, signatureName, extension).toUpperCase());
    jos.putNextEntry(bkJarEntry);

    // Write content
    ByteArrayInputStream bais = new ByteArrayInputStream(sigBlock);

    byte[] buffer = new byte[2048];
    int read = -1;

    while ((read = bais.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }

    jos.closeEntry();
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * 2jar?,??jar//from w  w w. j ava  2s .  co  m
 * @param baseJar
 * @param diffJar
 * @param outJar
 */
public static void unionJar(File baseJar, File diffJar, File outJar) throws IOException {
    File outParentFolder = outJar.getParentFile();
    if (!outParentFolder.exists()) {
        outParentFolder.mkdirs();
    }
    List<String> diffEntries = getZipEntries(diffJar);
    FileOutputStream fos = new FileOutputStream(outJar);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(baseJar);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (diffEntries.contains(name)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();

}

From source file:net.hasor.maven.ExecMojo.java

/**
 * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for all
 * classpath elements. Copied from surefire (ForkConfiguration#createJar())
 *
 * @param classPath List&lt;String> of all classpath elements.
 * @return/*from  ww w  .j av  a  2s .c  om*/
 * @throws IOException
 */
private File createJar(List<String> classPath, String mainClass) throws IOException {
    File file = File.createTempFile("maven-exec", ".jar");
    file.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(file);
    JarOutputStream jos = new JarOutputStream(fos);
    jos.setLevel(JarOutputStream.STORED);
    JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
    jos.putNextEntry(je);
    Manifest man = new Manifest();
    // we can't use StringUtils.join here since we need to add a '/' to
    // the end of directory entries - otherwise the jvm will ignore them.
    StringBuilder cp = new StringBuilder();
    for (String el : classPath) {
        // NOTE: if File points to a directory, this entry MUST end in '/'.
        cp.append(new URL(new File(el).toURI().toASCIIString()).toExternalForm() + " ");
    }
    man.getMainAttributes().putValue("Manifest-Version", "1.0");
    man.getMainAttributes().putValue("Class-Path", cp.toString().trim());
    man.getMainAttributes().putValue("Main-Class", mainClass);
    man.write(jos);
    jos.close();
    return file;
}

From source file:kr.motd.maven.exec.ExecMojo.java

/**
 * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for all
 * classpath elements. Copied from surefire (ForkConfiguration#createJar())
 *
 * @param classPath List&lt;String> of all classpath elements.
 * @return//from  ww  w.  j a  va 2  s  .c  o  m
 * @throws IOException
 */
private File createJar(List<String> classPath, String mainClass) throws IOException {
    File file = File.createTempFile("maven-exec", ".jar");
    file.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(file);
    JarOutputStream jos = new JarOutputStream(fos);
    jos.setLevel(JarOutputStream.STORED);
    JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
    jos.putNextEntry(je);

    Manifest man = new Manifest();

    // we can't use StringUtils.join here since we need to add a '/' to
    // the end of directory entries - otherwise the jvm will ignore them.
    StringBuilder cp = new StringBuilder();
    for (String el : classPath) {
        // NOTE: if File points to a directory, this entry MUST end in '/'.
        cp.append(new URL(new File(el).toURI().toASCIIString()).toExternalForm() + " ");
    }

    man.getMainAttributes().putValue("Manifest-Version", "1.0");
    man.getMainAttributes().putValue("Class-Path", cp.toString().trim());
    man.getMainAttributes().putValue("Main-Class", mainClass);

    man.write(jos);
    jos.close();

    return file;
}

From source file:com.evolveum.midpoint.provisioning.ucf.impl.ConnectorFactoryIcfImpl.java

/**
 * Test if provided file is connector bundle
 * //from w  w  w  . j  a v a  2 s .  c o m
 * @param file
 *            tested file
 * @return boolean
 */
private Boolean isThisJarFileBundle(File file) {
    // Startup tests
    if (null == file) {
        throw new IllegalArgumentException("No file is providied for bundle test.");
    }

    // Skip all processing if it is not a file
    if (!file.isFile()) {
        LOGGER.debug("This {} is not a file", file.getAbsolutePath());
        return false;
    }

    Properties prop = new Properties();
    JarFile jar = null;
    // Open jar file
    try {
        jar = new JarFile(file);
    } catch (IOException ex) {
        LOGGER.debug("Unable to read jar file: " + file.getAbsolutePath() + " [" + ex.getMessage() + "]");
        return false;
    }

    // read jar file
    InputStream is;
    try {
        JarEntry entry = new JarEntry("META-INF/MANIFEST.MF");
        is = jar.getInputStream(entry);
    } catch (IOException ex) {
        LOGGER.debug("Unable to fine MANIFEST.MF in jar file: " + file.getAbsolutePath() + " ["
                + ex.getMessage() + "]");
        return false;
    }

    // Parse jar file
    try {
        prop.load(is);
    } catch (IOException ex) {
        LOGGER.debug("Unable to parse jar file: " + file.getAbsolutePath() + " [" + ex.getMessage() + "]");
        return false;
    } catch (NullPointerException ex) {
        LOGGER.debug("Unable to parse jar file: " + file.getAbsolutePath() + " [" + ex.getMessage() + "]");
        return false;
    }

    // Test if it is a connector
    if (null != prop.get("ConnectorBundle-Name")) {
        LOGGER.info("Discovered ICF bundle in JAR: " + prop.get("ConnectorBundle-Name") + " version: "
                + prop.get("ConnectorBundle-Version"));
        return true;
    }

    LOGGER.debug("Provided file {} is not iCF bundle jar", file.getAbsolutePath());
    return false;
}

From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java

private static void addEntry(JarOutputStream jas, String name, byte[] content) throws Exception {
    JarEntry entry = new JarEntry(name);
    jas.putNextEntry(entry);//from www .  j a  v a 2  s  . c o m
    if (content != null) {
        jas.write(content);
    }
    jas.closeEntry();
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException {
    if (outputFile.exists()) {
        FileUtils.deleteQuietly(outputFile);
    }/*from  w w w.j a v a2 s . c o  m*/
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    FileOutputStream fos = new FileOutputStream(outputFile);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
}