Java ClassPath loadDirectoryFromClasspath(String path)

Here you can find the source of loadDirectoryFromClasspath(String path)

Description

load Directory From Classpath

License

Open Source License

Declaration

public static Set<String> loadDirectoryFromClasspath(String path) throws URISyntaxException, IOException 

Method Source Code


//package com.java2s;
/*/*from  ww w.j av  a 2 s  .  c  o  m*/
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * For information about the authors of this project Have a look
 * at the AUTHORS file in the root of this project.
 */

import java.io.File;
import java.io.IOException;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    public static Set<String> loadDirectoryFromClasspath(String path) throws URISyntaxException, IOException {
        ClassLoader cl = getContextClassLoader();
        Enumeration<URL> urls = cl.getResources(path);
        Set<String> children = new HashSet<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            URI uri = null;
            if ("jar".equals(url.getProtocol())) { //$NON-NLS-1$
                uri = new URI(url.toString().replaceAll("^jar:(.+)!/.*$", "$1")); //$NON-NLS-1$ //$NON-NLS-2$
            } else {
                uri = url.toURI();
            }
            File src = new File(uri);
            if (src.isDirectory() && src.exists()) {
                enumerateDirectoryChildren(children, src);
            } else if (src.isFile() && src.exists()) {
                enumerateJarEntriesWith(path, children, src);
            }
        }
        return children;
    }

    private static ClassLoader getContextClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    private static void enumerateDirectoryChildren(Set<String> children, File src) {
        String[] files = src.list();
        if (null != files) {
            for (String f : files) {
                children.add(f);
            }
        }
    }

    private static void enumerateJarEntriesWith(String path, Set<String> children, File src) throws IOException {
        try (JarFile jar = new JarFile(src)) {
            Enumeration<JarEntry> jarEntries = jar.entries();
            String prefix = path;
            if ('/' == prefix.charAt(0)) {
                prefix = prefix.substring(1);
            }
            while (jarEntries.hasMoreElements()) {
                JarEntry entry = jarEntries.nextElement();
                String name = entry.getName();
                if (!entry.isDirectory() && name.startsWith(prefix)) { //filter according to the path
                    name = name.substring(prefix.length());
                    children.add(name);
                }
            }
        }
    }
}

Related

  1. getResourceFromClasspath(final String fileName)
  2. getStreamFromClassPath(String source)
  3. getSystemClassPath()
  4. getSystemClasspathEntries()
  5. isInClassPath(String location)
  6. loadFromClasspath(Class clazz, String fileName)
  7. loadFromClasspath(final String configFileName)
  8. loadResourceFromClasspath(Class clazz, String resourceName)
  9. parseClassPath(String classpath)