Java URL Connection findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)

Here you can find the source of findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)

Description

find Resource In Jar Package

License

Open Source License

Declaration

private static void findResourceInJarPackage(URL url, String packageName, String packageDirName,
        boolean recursive, List<String> resources) throws IOException 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;

import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {

    private static void findResourceInJarPackage(URL url, String packageName, String packageDirName,
            boolean recursive, List<String> resources) throws IOException {
        JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.charAt(0) == '/')
                name = name.substring(1);
            if (name.startsWith(packageDirName)) {
                int idx = name.lastIndexOf('/');
                if (idx != -1)
                    packageName = name.substring(0, idx).replace('/', '.');
                if ((idx != -1) || recursive) {
                    // it's not inside a deeper dir
                    if (!entry.isDirectory())
                        resources.add(packageName + "." + name.substring(packageName.length() + 1));
                }//from   w  ww . j  ava  2  s.  c  o m
            }
        }
    }
}

Related

  1. doGet(String url, String charset)
  2. doPost(final URL url, final Map parameters, final boolean encode)
  3. doPost(String url, String params, String charset)
  4. findClasses(URL resource, String packageName, boolean annotated)
  5. findClasspathUrls(ClassLoader classLoader)
  6. getAsStream(URL url)
  7. getBaseAuthInputStreamFromURL(String query, String basicAuthString)
  8. getBaseURL(URLConnection conn)
  9. getBooleanFromUrl(String url)