Example usage for java.util.jar JarInputStream close

List of usage examples for java.util.jar JarInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:com.redhat.victims.plugin.ant.FileStub.java

/**
 * Creates metadata from a given jar file.
 * /*from   ww w  .  j  a  va  2s .  com*/
 * @param jar
 *            file containing a manifest
 * @return Metadata containing extracted information from manifest file.
 * @throws FileNotFoundException
 * @throws VictimsException
 */
public static Metadata getMeta(File jar) throws FileNotFoundException, VictimsException {
    if (!jar.getAbsolutePath().endsWith(".jar"))
        return null;
    JarInputStream jis = null;
    try {
        jis = new JarInputStream(new FileInputStream(jar));
        Manifest mf = jis.getManifest();
        jis.close();
        if (mf != null)
            return Metadata.fromManifest(mf);
    } catch (IOException io) {
        throw new VictimsException(String.format("Could not open file: %s", jar.getName()), io);
    } finally {
        IOUtils.closeQuietly(jis);
    }
    return null;
}

From source file:JarUtils.java

/**
 * Add jar contents to the deployment archive under the given prefix
 *///from ww  w .j a va2s.c  om
public static String[] addJar(JarOutputStream outputStream, String prefix, File jar) throws IOException {

    ArrayList tmp = new ArrayList();
    FileInputStream fis = new FileInputStream(jar);
    JarInputStream jis = new JarInputStream(fis);
    JarEntry entry = jis.getNextJarEntry();
    while (entry != null) {
        if (entry.isDirectory() == false) {
            String entryName = prefix + entry.getName();
            tmp.add(entryName);
            addJarEntry(outputStream, entryName, jis);
        }
        entry = jis.getNextJarEntry();
    }
    jis.close();
    String[] names = new String[tmp.size()];
    tmp.toArray(names);
    return names;
}

From source file:org.talend.designer.maven.utils.ClasspathsJarGenerator.java

public static String getClasspathFromManifest(Property property) throws Exception {
    String jarLocation = getJarLocation(property);
    JarInputStream stream = null;
    try {/*from w w  w .j  a  v  a2s.  c om*/
        stream = new JarInputStream(new FileInputStream(jarLocation));
        Manifest manifest = stream.getManifest();
        String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
        return classpath;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:org.apache.catalina.util.ExtensionValidator.java

/**
 * Return the Manifest from a jar file or war file
 *
 * @param inStream Input stream to a WAR or JAR file
 * @return The WAR's or JAR's manifest/* ww w .  ja va  2s.  co m*/
 */
private static Manifest getManifest(InputStream inStream) throws IOException {

    Manifest manifest = null;
    JarInputStream jin = null;

    try {
        jin = new JarInputStream(inStream);
        manifest = jin.getManifest();
        jin.close();
        jin = null;
    } finally {
        if (jin != null) {
            try {
                jin.close();
            } catch (Throwable t) {
                // Ignore
            }
        }
    }

    return manifest;
}

From source file:com.fengduo.bee.commons.core.lang.ClassLoaderUtils.java

public static List<String> getClassNamesInPackage(String jarName, String packageName) throws IOException {
    JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
    packageName = packageName.replace(".", "/");
    List<String> classes = new ArrayList<String>();

    try {//  w  w w .ja v a  2s .  c o  m
        for (JarEntry jarEntry; (jarEntry = jarFile.getNextJarEntry()) != null;) {
            if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) {
                classes.add(jarEntry.getName().replace("/", ".").replaceAll(".class", ""));
            }
        }
    } finally {
        jarFile.close();
    }
    return classes;
}

From source file:com.katsu.dwm.reflection.JarUtils.java

/**
 * Return as stream a resource in a jar//from  w w w . jav  a  2s  .c  o m
 * @param jar
 * @param resource 
 */
public static InputStream getResourceAsStreamFromJar(File jar, String resource) {
    JarInputStream jis = null;
    try {
        jis = new JarInputStream(new FileInputStream(jar));
        JarEntry je;
        while ((je = jis.getNextJarEntry()) != null) {
            logger.trace(jar.getName() + " " + je.getName());
            if (je.getName().equals(resource)) {
                return jis;
            }
        }
    } catch (Exception ex) {
        logger.error(ex + " " + jar.getPath());
    } finally {
        if (jis != null) {
            try {
                jis.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }
    return null;
}

From source file:org.diffkit.common.DKUnjar.java

/**
 * closes inputStream_ at the end/*from  ww  w .j  a  v a2s. c o  m*/
 */
public static void unjar(JarInputStream inputStream_, File outputDir_) throws IOException {
    DKValidate.notNull(inputStream_, outputDir_);
    if (!outputDir_.isDirectory())
        throw new RuntimeException(String.format("directory does not exist->%s", outputDir_));

    JarEntry entry = null;
    while ((entry = inputStream_.getNextJarEntry()) != null) {
        File outFile = new File(outputDir_, entry.getName());
        OutputStream outStream = new BufferedOutputStream(new FileOutputStream(outFile));
        DKStreamUtil.copy(inputStream_, outStream);
        outStream.flush();
        outStream.close();
    }
    inputStream_.close();
}

From source file:org.eclipse.gemini.blueprint.util.DebugUtils.java

private static URL checkBundleJarsForClass(Bundle bundle, String name) {
    String cname = name.replace('.', '/') + ".class";
    for (Enumeration e = bundle.findEntries("/", "*.jar", true); e != null && e.hasMoreElements();) {
        URL url = (URL) e.nextElement();
        JarInputStream jin = null;
        try {/* ww w  .j a  v a 2 s .  c  o  m*/
            jin = new JarInputStream(url.openStream());
            // Copy entries from the real jar to our virtual jar
            for (JarEntry ze = jin.getNextJarEntry(); ze != null; ze = jin.getNextJarEntry()) {
                if (ze.getName().equals(cname)) {
                    jin.close();
                    return url;
                }
            }
        } catch (IOException e1) {
            log.trace("Skipped " + url.toString() + ": " + e1.getMessage());
        }

        finally {
            if (jin != null) {
                try {
                    jin.close();
                } catch (Exception ex) {
                    // ignore it
                }
            }
        }

    }
    return null;
}

From source file:org.apache.geode.internal.DeployedJar.java

/**
 * Peek into the JAR data and make sure that it is valid JAR content.
 *
 * @param inputStream InputStream containing data to be validated.
 * @return True if the data has JAR content, false otherwise
 *//*from  w ww  .  ja  va2  s  .  c  o m*/
private static boolean hasValidJarContent(final InputStream inputStream) {
    JarInputStream jarInputStream = null;
    boolean valid = false;

    try {
        jarInputStream = new JarInputStream(inputStream);
        valid = jarInputStream.getNextJarEntry() != null;
    } catch (IOException ignore) {
        // Ignore this exception and just return false
    } finally {
        try {
            jarInputStream.close();
        } catch (IOException ignored) {
            // Ignore this exception and just return result
        }
    }

    return valid;
}

From source file:JarUtil.java

/**
 * Adds the given file to the specified JAR file.
 * /* w w  w . j  a  v a2  s . c o  m*/
 * @param file
 *            the file that should be added
 * @param jarFile
 *            The JAR to which the file should be added
 * @param parentDir
 *            the parent directory of the file, this is used to calculate
 *            the path witin the JAR file. When null is given, the file will
 *            be added into the root of the JAR.
 * @param compress
 *            True when the jar file should be compressed
 * @throws FileNotFoundException
 *             when the jarFile does not exist
 * @throws IOException
 *             when a file could not be written or the jar-file could not
 *             read.
 */
public static void addToJar(File file, File jarFile, File parentDir, boolean compress)
        throws FileNotFoundException, IOException {
    File tmpJarFile = File.createTempFile("tmp", ".jar", jarFile.getParentFile());
    JarOutputStream out = new JarOutputStream(new FileOutputStream(tmpJarFile));
    if (compress) {
        out.setLevel(ZipOutputStream.DEFLATED);
    } else {
        out.setLevel(ZipOutputStream.STORED);
    }
    // copy contents of old jar to new jar:
    JarFile inputFile = new JarFile(jarFile);
    JarInputStream in = new JarInputStream(new FileInputStream(jarFile));
    CRC32 crc = new CRC32();
    byte[] buffer = new byte[512 * 1024];
    JarEntry entry = (JarEntry) in.getNextEntry();
    while (entry != null) {
        InputStream entryIn = inputFile.getInputStream(entry);
        add(entry, entryIn, out, crc, buffer);
        entryIn.close();
        entry = (JarEntry) in.getNextEntry();
    }
    in.close();
    inputFile.close();

    int sourceDirLength;
    if (parentDir == null) {
        sourceDirLength = file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1;
    } else {
        sourceDirLength = file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1
                - parentDir.getAbsolutePath().length();
    }
    addFile(file, out, crc, sourceDirLength, buffer);
    out.close();

    // remove old jar file and rename temp file to old one:
    if (jarFile.delete()) {
        if (!tmpJarFile.renameTo(jarFile)) {
            throw new IOException(
                    "Unable to rename temporary JAR file to [" + jarFile.getAbsolutePath() + "].");
        }
    } else {
        throw new IOException("Unable to delete old JAR file [" + jarFile.getAbsolutePath() + "].");
    }

}