Here you can find the source of getClassPathArchiveContents(final URL resource)
resource
represents a classpath archive (i.e.
Parameter | Description |
---|---|
resource | the resource from which to retrieve the contents |
public static List<String> getClassPathArchiveContents(final URL resource)
//package com.java2s; //License from project: BSD License import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main { /**//from w w w . java 2 s . com * All archive files start with this prefix. */ private static final String ARCHIVE_PREFIX = "jar:"; private static final String URL_DECODE_ENCODING = "UTF-8"; /** * If the <code>resource</code> represents a classpath archive (i.e. jar, zip, etc), this method will retrieve all * contents from that resource as a List of relative paths (relative to the archive base). Otherwise an empty List * will be returned. * * @param resource the resource from which to retrieve the contents * @return a list of Strings containing the names of every nested resource found in this resource. */ public static List<String> getClassPathArchiveContents(final URL resource) { final List<String> contents = new ArrayList<String>(); if (isArchive(resource)) { final ZipFile archive = getArchive(resource); if (archive != null) { for (final Enumeration<? extends ZipEntry> entries = archive.entries(); entries .hasMoreElements();) { final ZipEntry entry = entries.nextElement(); contents.add(entry.getName()); } try { archive.close(); } catch (IOException ex) { // Ignore } } } return contents; } /** * Returns true/false on whether or not this <code>resource</code> represents an archive or not (i.e. jar, or zip, * etc). * * @param resource * @return true if its an archive, false otherwise. */ public static boolean isArchive(final URL resource) { return resource != null && resource.toString().startsWith(ARCHIVE_PREFIX); } /** * If this <code>resource</code> is an archive file, it will return the resource as an archive. * * @param resource * @return the archive as a ZipFile */ public static ZipFile getArchive(final URL resource) { try { ZipFile archive = null; if (resource != null) { String resourceUrl = resource.toString(); resourceUrl = resourceUrl.replaceFirst(ARCHIVE_PREFIX, ""); final int entryPrefixIndex = resourceUrl.indexOf('!'); if (entryPrefixIndex != -1) { resourceUrl = resourceUrl.substring(0, entryPrefixIndex); } resourceUrl = URLDecoder.decode(new URL(resourceUrl).getFile(), URL_DECODE_ENCODING); File zipFile = new File(resourceUrl); if (zipFile.exists()) { archive = new ZipFile(resourceUrl); } else { // ZipFile doesn't give enough detail about missing file throw new FileNotFoundException("ResourceUtils.getArchive " + resourceUrl + " NOT FOUND."); } } return archive; } // Don't unnecessarily wrap RuntimeException catch (final RuntimeException ex) { throw ex; } // But don't require Exception declaration either. catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }