Java Resource Path Get getResourceListing(Class clazz, String path)

Here you can find the source of getResourceListing(Class clazz, String path)

Description

get Resource Listing

License

Open Source License

Declaration

@SuppressWarnings("resource")
    public static String[] getResourceListing(Class<?> clazz, String path) throws URISyntaxException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;

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 {
    @SuppressWarnings("resource")
    public static String[] getResourceListing(Class<?> clazz, String path) throws URISyntaxException, IOException {
        URL dirURL = clazz.getClassLoader().getResource(path);
        if (dirURL != null && dirURL.getProtocol().equals("file")) {
            /* A file path: easy enough */
            return new File(dirURL.toURI()).list();
        }//from  ww w.jav a2s  .  c  o  m

        if (dirURL == null) {
            /*
             * In case of a jar file, we can't actually find a directory. Have
             * to assume the same jar as clazz.
             */
            String me = clazz.getName().replace(".", "/") + ".class";
            dirURL = clazz.getClassLoader().getResource(me);
        }

        if (dirURL.getProtocol().equals("jar")) {
            /* A JAR path */
            String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); // strip out only the JAR
            // file
            JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
            Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries
            // in jar
            Set<String> result = new HashSet<String>(); // avoid duplicates in
            // case it is a
            // subdirectory
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.startsWith(path)) { // filter according to the path
                    String entry = name.substring(path.length());
                    int checkSubdir = entry.indexOf("/");
                    if (checkSubdir >= 0) {
                        // if it is a subdirectory, we just return the directory
                        // name
                        entry = entry.substring(0, checkSubdir);
                    }
                    result.add(entry);
                }
            }
            return result.toArray(new String[result.size()]);
        }

        throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
    }
}

Related

  1. getResourceFile(final Class baseClass, final String path)
  2. getResourceFile(String sResourcePath, Class cRefClass)
  3. getResourceFilePath(String name)
  4. getResourceFileRelativeToBase(final File baseDir, final String resourcePath)
  5. getResourceListing(Class clazz, String path)
  6. getResourceListing(Class clazz, String path)
  7. getResourceListing(Class clazz, String path, String glob)
  8. getResourcePath()
  9. getResourcePath(Class clazz, String fileName)