Java URL Connection loadJar(String path, URL resource)

Here you can find the source of loadJar(String path, URL resource)

Description

load Jar

License

Apache License

Declaration

public static List<URL> loadJar(String path, URL resource) throws IOException 

Method Source Code

//package com.java2s;
/*//from www  .  j  a v  a  2 s.  c om
 * Copyright 2009-2016 DigitalGlobe, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 */

import java.io.IOException;

import java.net.JarURLConnection;

import java.net.URL;

import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    public static List<URL> loadJar(String path, URL resource) throws IOException {
        JarURLConnection conn = (JarURLConnection) resource.openConnection();
        JarFile jarFile = conn.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
        List<URL> result = new LinkedList<URL>();

        String p = path;
        if (p.endsWith("/") == false) {
            p = p + "/";
        }

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if ((!entry.getName().equals(p))
                    && (entry.getName().startsWith(p) || entry.getName().startsWith("WEB-INF/classes/" + p))) {
                URL url = new URL("jar:" + new URL("file", null, jarFile.getName() + "!/" + entry.getName()));
                result.add(url);
            }
        }

        return result;
    }
}

Related

  1. isZipName(URI documentIRI, URLConnection connection)
  2. jarURLDirectories(URL jarURL)
  3. jarURLEntryResource(String jarURLString)
  4. lastModifiedURL(String urlstr)
  5. loadCookie(URL url)
  6. loadLinesUrl(String url)
  7. loadObjectFromURL(URL url)
  8. map2URL(final Properties properties)
  9. parseURL(ClassLoader classLoader, String uri)