Search in sources :

Example 71 with JarFile

use of java.util.jar.JarFile in project reflections by ronmamo.

the class ClasspathHelper method forManifest.

/**
     * Returns a distinct collection of URLs from a single URL based on the Manifest information.
     * <p>
     * The {@code MANIFEST.MF} file can contain a {@code Class-Path} entry that defines additional
     * jar files to be included on the classpath. This method takes a single URL, tries to
     * resolve it as a jar file, and if so, adds any additional manifest classpaths.
     * The returned collection of URLs will always contain the input URL.
     * 
     * @return the collection of URLs, not null
     */
public static Collection<URL> forManifest(final URL url) {
    final Collection<URL> result = new ArrayList<URL>();
    result.add(url);
    try {
        final String part = cleanPath(url);
        File jarFile = new File(part);
        JarFile myJar = new JarFile(part);
        URL validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), part);
        if (validUrl != null) {
            result.add(validUrl);
        }
        final Manifest manifest = myJar.getManifest();
        if (manifest != null) {
            final String classPath = manifest.getMainAttributes().getValue(new Attributes.Name("Class-Path"));
            if (classPath != null) {
                for (String jar : classPath.split(" ")) {
                    validUrl = tryToGetValidUrl(jarFile.getPath(), new File(part).getParent(), jar);
                    if (validUrl != null) {
                        result.add(validUrl);
                    }
                }
            }
        }
    } catch (IOException e) {
    // don't do anything, we're going on the assumption it is a jar, which could be wrong
    }
    return distinctUrls(result);
}
Also used : Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Example 72 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class URLClassLoader method createURLJarHandler.

private URLHandler createURLJarHandler(URL url) {
    String prefixName;
    String file = url.getFile();
    if (url.getFile().endsWith("!/")) {
        prefixName = "";
    } else {
        int sepIdx = file.lastIndexOf("!/");
        if (sepIdx == -1) {
            // Invalid URL, don't look here again
            return null;
        }
        sepIdx += 2;
        prefixName = file.substring(sepIdx);
    }
    try {
        URL jarURL = ((JarURLConnection) url.openConnection()).getJarFileURL();
        JarURLConnection juc = (JarURLConnection) new URL("jar", "", jarURL.toExternalForm() + "!/").openConnection();
        JarFile jf = juc.getJarFile();
        URLJarHandler jarH = new URLJarHandler(url, jarURL, jf, prefixName);
        if (jarH.getIndex() == null) {
            try {
                Manifest manifest = jf.getManifest();
                if (manifest != null) {
                    String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
                    if (classpath != null) {
                        searchList.addAll(0, getInternalURLs(url, classpath));
                    }
                }
            } catch (IOException e) {
            }
        }
        return jarH;
    } catch (IOException e) {
    }
    return null;
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest)

Example 73 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class TrustedCertificateStore method list.

private String[] list(URI file) {
    if ("file".equals(file.getScheme())) {
        return new File(file).list();
    }
    if ("jar".equals(file.getScheme())) {
        try {
            JarURLConnection conn = (JarURLConnection) file.toURL().openConnection();
            JarFile jarFile = conn.getJarFile();
            String uriStr = file.toString();
            if (!uriStr.endsWith("/")) {
                uriStr += "/";
            }
            String path = uriStr.substring(uriStr.lastIndexOf('!') + 1);
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            Enumeration<JarEntry> en = jarFile.entries();
            List<String> result = new ArrayList<String>();
            while (en.hasMoreElements()) {
                JarEntry entry = en.nextElement();
                String name = entry.getName();
                if (name.startsWith(path) && !name.endsWith("/")) {
                    int lastSlash = name.lastIndexOf('/');
                    if (lastSlash == path.length() - 1) {
                        result.add(name.substring(lastSlash + 1));
                    }
                }
            }
            return result.toArray(new String[result.size()]);
        } catch (IOException e) {
        }
    }
    return null;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 74 with JarFile

use of java.util.jar.JarFile in project spring-boot by spring-projects.

the class ExplodedArchiveTests method createArchive.

private void createArchive(String folderName) throws Exception {
    File file = this.temporaryFolder.newFile();
    TestJarCreator.createTestJar(file);
    this.rootFolder = StringUtils.hasText(folderName) ? this.temporaryFolder.newFolder(folderName) : this.temporaryFolder.newFolder();
    JarFile jarFile = new JarFile(file);
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        File destination = new File(this.rootFolder.getAbsolutePath() + File.separator + entry.getName());
        destination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            destination.mkdir();
        } else {
            copy(jarFile.getInputStream(entry), new FileOutputStream(destination));
        }
    }
    this.archive = new ExplodedArchive(this.rootFolder);
    jarFile.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 75 with JarFile

use of java.util.jar.JarFile in project spring-boot by spring-projects.

the class AbstractExecutableArchiveLauncherTests method explode.

protected File explode(File archive) throws IOException {
    File exploded = this.temp.newFolder("exploded");
    JarFile jarFile = new JarFile(archive);
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        File entryFile = new File(exploded, entry.getName());
        if (entry.isDirectory()) {
            entryFile.mkdirs();
        } else {
            FileCopyUtils.copy(jarFile.getInputStream(entry), new FileOutputStream(entryFile));
        }
    }
    jarFile.close();
    return exploded;
}
Also used : FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarFile (java.util.jar.JarFile)509 File (java.io.File)289 JarEntry (java.util.jar.JarEntry)230 IOException (java.io.IOException)212 URL (java.net.URL)92 ZipEntry (java.util.zip.ZipEntry)92 InputStream (java.io.InputStream)90 Manifest (java.util.jar.Manifest)81 ZipFile (java.util.zip.ZipFile)74 FileOutputStream (java.io.FileOutputStream)71 Test (org.junit.Test)66 ArrayList (java.util.ArrayList)54 JarURLConnection (java.net.JarURLConnection)48 Attributes (java.util.jar.Attributes)43 JarOutputStream (java.util.jar.JarOutputStream)41 MalformedURLException (java.net.MalformedURLException)31 FileInputStream (java.io.FileInputStream)30 Support_PlatformFile (tests.support.Support_PlatformFile)26 Enumeration (java.util.Enumeration)24 HashSet (java.util.HashSet)20