Search in sources :

Example 21 with JarInputStream

use of java.util.jar.JarInputStream in project gocd by gocd.

the class TfsSDKCommandBuilderTest method implementationVersionFromManifrest.

private String implementationVersionFromManifrest(URL log4jJarFromClasspath) throws IOException {
    JarInputStream in = new JarInputStream(log4jJarFromClasspath.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    in.getManifest().write(out);
    out.close();
    List<String> lines = IOUtils.readLines(new ByteArrayInputStream(out.toByteArray()));
    for (String line : lines) {
        if (line.startsWith("Implementation-Version")) {
            return line.split(":")[1].trim();
        }
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with JarInputStream

use of java.util.jar.JarInputStream in project hibernate-orm by hibernate.

the class JarFileBasedArchiveDescriptor method visitArchive.

@Override
public void visitArchive(ArchiveContext context) {
    final JarFile jarFile = resolveJarFileReference();
    if (jarFile == null) {
        return;
    }
    final Enumeration<? extends ZipEntry> zipEntries = jarFile.entries();
    while (zipEntries.hasMoreElements()) {
        final ZipEntry zipEntry = zipEntries.nextElement();
        final String entryName = extractName(zipEntry);
        if (getEntryBasePrefix() != null && !entryName.startsWith(getEntryBasePrefix())) {
            continue;
        }
        if (zipEntry.isDirectory()) {
            continue;
        }
        if (entryName.equals(getEntryBasePrefix())) {
            // just any random entry
            try (InputStream is = new BufferedInputStream(jarFile.getInputStream(zipEntry))) {
                final JarInputStream jarInputStream = new JarInputStream(is);
                ZipEntry subZipEntry = jarInputStream.getNextEntry();
                while (subZipEntry != null) {
                    if (!subZipEntry.isDirectory()) {
                        final String name = extractName(subZipEntry);
                        final String relativeName = extractRelativeName(subZipEntry);
                        final InputStreamAccess inputStreamAccess = buildByteBasedInputStreamAccess(name, jarInputStream);
                        final ArchiveEntry entry = new ArchiveEntry() {

                            @Override
                            public String getName() {
                                return name;
                            }

                            @Override
                            public String getNameWithinArchive() {
                                return relativeName;
                            }

                            @Override
                            public InputStreamAccess getStreamAccess() {
                                return inputStreamAccess;
                            }
                        };
                        final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler(entry);
                        entryHandler.handleEntry(entry, context);
                    }
                    subZipEntry = jarInputStream.getNextEntry();
                }
            } catch (Exception e) {
                throw new ArchiveException("Error accessing JarFile entry [" + zipEntry.getName() + "]", e);
            }
        } else {
            final String name = extractName(zipEntry);
            final String relativeName = extractRelativeName(zipEntry);
            final InputStreamAccess inputStreamAccess;
            try (InputStream is = jarFile.getInputStream(zipEntry)) {
                inputStreamAccess = buildByteBasedInputStreamAccess(name, is);
            } catch (IOException e) {
                throw new ArchiveException(String.format("Unable to access stream from jar file [%s] for entry [%s]", jarFile.getName(), zipEntry.getName()));
            }
            final ArchiveEntry entry = new ArchiveEntry() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getNameWithinArchive() {
                    return relativeName;
                }

                @Override
                public InputStreamAccess getStreamAccess() {
                    return inputStreamAccess;
                }
            };
            final ArchiveEntryHandler entryHandler = context.obtainArchiveEntryHandler(entry);
            entryHandler.handleEntry(entry, context);
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) InputStreamAccess(org.hibernate.boot.archive.spi.InputStreamAccess) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArchiveEntry(org.hibernate.boot.archive.spi.ArchiveEntry) ArchiveEntryHandler(org.hibernate.boot.archive.spi.ArchiveEntryHandler) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) ArchiveException(org.hibernate.boot.archive.spi.ArchiveException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream)

Example 23 with JarInputStream

use of java.util.jar.JarInputStream in project lwjgl by LWJGL.

the class StandalonePublisher method readBundle.

protected BundleInfo readBundle(File location) throws ParserConfigurationException {
    BundleInfo info = new BundleInfo();
    info.jarFile = location;
    info.size = (int) location.length();
    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    JarInputStream jis = null;
    try {
        jis = new JarInputStream(new FileInputStream(location));
        info.manifest = jis.getManifest();
        JarEntry jarEntry;
        while ((jarEntry = jis.getNextJarEntry()) != null) {
            if (!jarEntry.isDirectory() && "plugin.xml".equals(jarEntry.getName())) {
                Document doc = readXMLinJar(docBuilder, jis, jarEntry);
                info.doc = doc;
                break;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (jis != null)
            try {
                jis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    }
    return info;
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 24 with JarInputStream

use of java.util.jar.JarInputStream in project OpenAM by OpenRock.

the class TestHarness method getTestClasses.

public static void getTestClasses(ServletContext servletContext, Map map) {
    JarInputStream in = null;
    try {
        in = new JarInputStream(servletContext.getResourceAsStream("WEB-INF/lib/unittest.jar"));
        JarEntry jarEntry = in.getNextJarEntry();
        while (jarEntry != null) {
            String name = jarEntry.getName();
            if (name.endsWith(".class") && (name.indexOf("$") == -1)) {
                name = name.replaceAll("/", ".");
                int idx = name.lastIndexOf('.');
                name = name.substring(0, idx);
                idx = name.lastIndexOf('.');
                String pkgName = name.substring(0, idx);
                if (!pkgName.equals("com.sun.identity.unittest")) {
                    Set set = (Set) map.get(pkgName);
                    if (set == null) {
                        set = new TreeSet();
                        map.put(pkgName, set);
                    }
                    set.add(name);
                }
            }
            jarEntry = in.getNextJarEntry();
        }
    } catch (IOException e) {
        UnittestLog.logError("TestHarness.getTestClasses: failed", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            //ignore
            }
        }
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) JarInputStream(java.util.jar.JarInputStream) TreeSet(java.util.TreeSet) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Example 25 with JarInputStream

use of java.util.jar.JarInputStream in project OpenAM by OpenRock.

the class FilesDigester method digestJarFile.

/**
     * This function calculate the hash value of a jar file.
     *
     * This function handles the jar file as a concatenation of the decompressed
     * files it contains.
     *
     * @param hashAlg The algorithm to be used for calculating the hash.
     * @param in The InputStream of the jar file to be processed.
     */
protected byte[] digestJarFile(String hashAlg, InputStream in) {
    JarInputStream jin = null;
    try {
        jin = new JarInputStream(in);
        JarEntry je = null;
        MessageDigest md = MessageDigest.getInstance(hashAlg);
        while ((je = jin.getNextJarEntry()) != null) {
            if (!je.isDirectory()) {
                md = Utils.hashing(md, jin);
            }
            jin.closeEntry();
        }
        jin.close();
        return md.digest();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    } finally {
        if (jin != null) {
            try {
                jin.close();
            } catch (IOException ignored) {
            }
            jin = null;
        }
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JarEntry(java.util.jar.JarEntry) MessageDigest(java.security.MessageDigest)

Aggregations

JarInputStream (java.util.jar.JarInputStream)154 JarEntry (java.util.jar.JarEntry)72 IOException (java.io.IOException)66 FileInputStream (java.io.FileInputStream)63 Manifest (java.util.jar.Manifest)50 File (java.io.File)46 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)29 JarOutputStream (java.util.jar.JarOutputStream)27 FileOutputStream (java.io.FileOutputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)22 URL (java.net.URL)20 Test (org.junit.Test)20 OutputStream (java.io.OutputStream)13 JarFile (java.util.jar.JarFile)13 BufferedInputStream (java.io.BufferedInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ArrayList (java.util.ArrayList)10 Attributes (java.util.jar.Attributes)10 HashSet (java.util.HashSet)8