Description
List directory contents for a resource folder.
License
Open Source License
Parameter
Parameter | Description |
---|
clazz | Any java class that lives in the same place as the resources you want. |
path | Should end with "/", but not start with one. |
Exception
Parameter | Description |
---|
URISyntaxException | an exception |
IOException | an exception |
Return
List of URLs pointing to all subentries including the specified one.
Declaration
public static Map<String, URL> getResourceListing(Class clazz, String path)
throws URISyntaxException, IOException
Method Source Code
//package com.java2s;
/****************************************************************************
* Copyright (C) 2012 ecsec GmbH.//ww w . j av a 2s .c o m
* All rights reserved.
* Contact: ecsec GmbH (info@ecsec.de)
*
* This file is part of the Open eCard App.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License version 3.0 as published by the Free Software Foundation
* and appearing in the file LICENSE.GPL included in the packaging of
* this file. Please review the following information to ensure the
* GNU General Public License version 3.0 requirements will be met:
* http://www.gnu.org/copyleft/gpl.html.
*
* Other Usage
* Alternatively, this file may be used in accordance with the terms
* and conditions contained in a signed written agreement between
* you and ecsec GmbH.
*
***************************************************************************/
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
/**
* List directory contents for a resource folder. This is basically a brute-force implementation.
* Works for regular files and also JARs. <p>Taken from
* {@link http://www.uofr.net/~greg/java/get-resource-listing.html} and modified for our needs.</p>
*
* @author Greg Briggs
* @param clazz Any java class that lives in the same place as the resources you want.
* @param path Should end with "/", but not start with one.
* @return List of URLs pointing to all subentries including the specified one.
* @throws URISyntaxException
* @throws IOException
*/
public static Map<String, URL> getResourceListing(Class clazz, String path)
throws URISyntaxException, IOException {
URL dirURL = clazz.getClassLoader().getResource(path);
if (dirURL != null && dirURL.getProtocol().equals("file")) {
File dirFile = new File(dirURL.toURI());
return getSubdirFileListing(dirFile, dirURL.toExternalForm());
}
// TODO: I think this code is not needed (at least on linux), revise on windows and remove if possible
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
String jarUrl = dirURL.toExternalForm().substring(0, dirURL.toExternalForm().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
TreeMap<String, URL> result = new TreeMap<String, URL>(); //avoid duplicates in case it is a subdirectory
while (entries.hasMoreElements()) {
JarEntry nextEntry = entries.nextElement();
// skip directory entries
if (!nextEntry.isDirectory()) {
String name = nextEntry.getName();
if (name.startsWith(path)) { //filter according to the path
String entryPath = jarUrl + "!/" + name;
String prefix = "/" + name.substring(path.length());
result.put(prefix, new URL(entryPath));
}
}
}
return result;
}
throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}
private static TreeMap<String, URL> getSubdirFileListing(File dir, String base) throws MalformedURLException {
TreeMap<String, URL> resultList = new TreeMap<String, URL>();
for (File next : dir.listFiles()) {
if (next.canRead() && next.isDirectory()) {
resultList.putAll(getSubdirFileListing(next, base));
} else if (next.canRead() && next.isFile()) {
// generate prefix
URL fileURL = next.toURI().toURL();
String prefix = fileURL.toExternalForm().substring(base.length() - 1);
resultList.put(prefix, fileURL);
}
}
return resultList;
}
}
Related
- getResourceFile(Class clazz, String relPath)
- getResourceFile(final Class> baseClass, final String path)
- getResourceFile(String sResourcePath, Class> cRefClass)
- getResourceFilePath(String name)
- getResourceFileRelativeToBase(final File baseDir, final String resourcePath)
- getResourceListing(Class> clazz, String path)
- getResourceListing(Class> clazz, String path)
- getResourceListing(Class> clazz, String path, String glob)
- getResourcePath()