Example usage for java.util.jar JarFile getInputStream

List of usage examples for java.util.jar JarFile getInputStream

Introduction

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

Prototype

public synchronized InputStream getInputStream(ZipEntry ze) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:com.util.InstallUtil.java

private Properties getDefaultProperties() throws IOException {
    String os = System.getProperty("os.name");

    String jar = ServerUtil.getPath() + "/install/installer.jar";
    String fileName = "data/installDefaultValues.properties";

    if (os.startsWith("Linux")) {
        fileName = "data/installDefaultValuesNoUI.properties";
    }/*from  w w  w  .  j  av  a 2 s .c  om*/

    JarFile jarFile = new JarFile(jar);
    JarEntry entry = jarFile.getJarEntry(fileName);
    InputStream input = jarFile.getInputStream(entry);

    Properties p = new Properties();
    p.load(input);

    jarFile.close();
    return p;
}

From source file:org.jvnet.hudson.update_center.LocalHPI.java

@Override
public File resolvePOM() throws IOException {
    JarFile jar = null;
    InputStream in = null;/*from  w  ww. jav  a 2  s .c o m*/
    OutputStream out = null;
    try {
        jar = new JarFile(jarFile);
        String pomPath = String.format("META-INF/maven/%s/%s/pom.xml", artifact.groupId, artifact.artifactId);
        ZipEntry e = jar.getEntry(pomPath);
        if (e == null) {
            return null;
        }

        File temporaryFile = File.createTempFile(artifact.artifactId, ".xml");
        temporaryFile.deleteOnExit();

        in = jar.getInputStream(e);
        out = new FileOutputStream(temporaryFile);

        IOUtils.copy(in, out);

        return temporaryFile;
    } finally {
        if (out != null) {
            out.close();
        }
        if (in != null) {
            in.close();
        }
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:org.sonatype.maven.plugin.emma4it.InstrumentProjectArtifactMojo.java

private void append(JarOutputStream output, File jar, boolean excludeMetainf) throws IOException {
    JarFile ejar = new JarFile(jar);

    Enumeration<JarEntry> entries = ejar.entries();
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        if (jarEntry.isDirectory() || (excludeMetainf && jarEntry.getName().startsWith("META-INF"))) {
            continue;
        }/*from ww w .  ja v  a 2 s. c  o m*/

        output.putNextEntry(jarEntry);

        InputStream input = ejar.getInputStream(jarEntry);
        try {
            IOUtil.copy(input, output);
        } finally {
            IOUtil.close(input);
        }

        output.flush();
    }

}

From source file:org.abs_models.backend.erlang.ErlApp.java

private void copyJarDirectory(JarFile jarFile, String inname, String outname) throws IOException {
    InputStream is = null;/*  w w w. jav a 2  s  . c o m*/
    for (JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(inname)) {
            String relFilename = entry.getName().substring(inname.length());
            if (!entry.isDirectory()) {
                is = jarFile.getInputStream(entry);
                ByteStreams.copy(is, Files.asByteSink(new File(outname, relFilename)).openStream());
            } else {
                new File(outname, relFilename).mkdirs();
            }
        }
    }
    is.close();
}

From source file:com.qualogy.qafe.web.css.util.CssProvider.java

private Map<String, InputStream> findResourceInFile(File resourceFile, String fileNamePattern)
        throws IOException {
    Map<String, InputStream> result = new TreeMap<String, InputStream>();
    if (resourceFile.canRead() && resourceFile.getAbsolutePath().endsWith(".jar")) {
        JarFile jarFile = new JarFile(resourceFile);
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry singleEntry = entries.nextElement();
            if (singleEntry.getName().matches(fileNamePattern)) {
                result.put(jarFile.getName() + "/" + singleEntry.getName(),
                        jarFile.getInputStream(singleEntry));
            }// w w  w  .j a va  2s.  c o m
        }
    }
    return result;
}

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

private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOException {
    try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(jarFile.getInputStream(entry))) {
        if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
            new CrcAndSize(inputStream).setupStoredEntry(entry);
        } else {/*from   w  ww  . j ava  2 s  .c  o  m*/
            entry.setCompressedSize(-1);
        }
    }
}

From source file:org.ebayopensource.turmeric.eclipse.maven.core.utils.MavenCoreUtils.java

/**
 * Gets the input stream from jar.//from w  w w .  j a va  2  s  .c o m
 *
 * @param mProject the m project
 * @param jarEntryPath the jar entry path
 * @return The input stream for the specified jar entry
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static InputStream getInputStreamFromJar(final MavenProject mProject, final String jarEntryPath)
        throws IOException {
    if (SOALogger.DEBUG)
        logger.entering(mProject, jarEntryPath);
    final File file = getJarFileForService(mProject);

    InputStream io = null;
    if (file.exists() && file.canRead()) {
        final JarFile jarFile = new JarFile(file);
        final JarEntry jarEntry = jarFile.getJarEntry(jarEntryPath);
        if (jarEntry != null) {
            io = jarFile.getInputStream(jarEntry);
        } else {
            logger.warning("Can not find the jar entry->" + jarEntryPath);
        }
    } else {
        logger.warning("Jar file is either not exist or not readable ->" + file);
    }
    if (SOALogger.DEBUG)
        logger.exiting(io);
    return io;
}

From source file:jp.co.tis.gsp.tools.dba.mojo.ImportSchemaMojo.java

/**
 * Jar?????//from w ww.  j  a  va 2s  .c  om
 * 
 * @param jar
 * @param destDir
 * @throws IOException
 */
private void extractJarAll(JarFile jar, String destDir) throws IOException {
    Enumeration<JarEntry> enumEntries = jar.entries();
    while (enumEntries.hasMoreElements()) {
        java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
        java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
        if (file.isDirectory()) {
            f.mkdir();
            continue;
        }
        java.io.InputStream is = jar.getInputStream(file);
        java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
        while (is.available() > 0) {
            fos.write(is.read());
        }
        fos.close();
        is.close();
    }
}

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

/** Add the SHA1 of every file to the manifest, creating it if necessary. */
private static Manifest addDigestsToManifest(final JarFile jar) throws IOException, GeneralSecurityException {
    final Manifest input = jar.getManifest();
    final Manifest output = new Manifest();

    final Attributes main = output.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    // We sort the input entries by name, and add them to the output manifest in sorted order.
    // We expect that the output map will be deterministic.
    final TreeMap<String, JarEntry> byName = new TreeMap<>();
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        byName.put(entry.getName(), entry);
    }//from w w  w  .ja v a  2 s. c  o m

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final byte[] buffer = new byte[4096];
    int num;

    for (JarEntry entry : byName.values()) {
        final String name = entry.getName();
        if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME)
                && !name.equals(CERT_RSA_NAME)) {
            InputStream data = jar.getInputStream(entry);
            while ((num = data.read(buffer)) > 0) {
                md.update(buffer, 0, num);
            }

            Attributes attr = null;
            if (input != null) {
                attr = input.getAttributes(name);
            }
            attr = attr != null ? new Attributes(attr) : new Attributes();
            attr.putValue("SHA1-Digest", base64encode(md.digest()));
            output.getEntries().put(name, attr);
        }
    }

    return output;
}

From source file:org.ormma.controller.OrmmaAssetController.java

/**
 * Copy text file from jar into asset directory.
 * /*from ww  w. j  a va 2s .  co  m*/
 * @param alias
 *            the alias to store it in
 * @param source
 *            the source
 * @return the path to the copied asset
 */
public String copyTextFromJarIntoAssetDir(String alias, String source) {
    InputStream in = null;
    try {
        URL url = OrmmaAssetController.class.getClassLoader().getResource(source);
        String file = url.getFile();
        if (file.startsWith("file:")) {
            file = file.substring(5);
        }
        int pos = file.indexOf("!");
        if (pos > 0)
            file = file.substring(0, pos);
        JarFile jf = new JarFile(file);
        JarEntry entry = jf.getJarEntry(source);
        in = jf.getInputStream(entry);
        String name = writeToDisk(in, alias, false);
        return name;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            in = null;
        }
    }
    return null;
}