11package org .utplsql .api ;
22
3+ import com .sun .nio .zipfs .ZipFileSystem ;
4+ import com .sun .nio .zipfs .ZipPath ;
35import org .utplsql .api .reporter .CoverageHTMLReporter ;
46
7+ import java .io .File ;
58import java .io .IOException ;
69import java .net .URI ;
710import java .net .URISyntaxException ;
811import java .nio .file .*;
912import java .util .ArrayList ;
1013import java .util .Collections ;
14+ import java .util .Enumeration ;
1115import java .util .List ;
16+ import java .util .zip .ZipEntry ;
17+ import java .util .zip .ZipFile ;
1218
13- /** Helper class for dealing with Resources
19+ /**
20+ * Helper class for dealing with Resources
1421 *
1522 * @author pesse
1623 */
1724public class ResourceUtil {
1825
19- /** Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
26+ /**
27+ * Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
2028 *
2129 * @param resourceName The name of the resource
2230 * @return Path to the resource, either in JAR or on file system
2331 * @throws IOException
2432 * @throws URISyntaxException
2533 */
26- public static Path getPathToResource ( String resourceName ) throws IOException , URISyntaxException {
34+ public static Path getPathToResource (String resourceName ) throws IOException , URISyntaxException {
2735 URI uri = CoverageHTMLReporter .class .getResource (resourceName ).toURI ();
2836 Path myPath ;
2937 if (uri .getScheme ().equalsIgnoreCase ("jar" )) {
@@ -36,26 +44,41 @@ public static Path getPathToResource( String resourceName ) throws IOException,
3644 return myPath ;
3745 }
3846
39- /** Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
47+ /**
48+ * Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
4049 *
4150 * @param resourceAsPath The resource to get children from
42- * @param filesOnly If set to true it will only return files, not directories
51+ * @param filesOnly If set to true it will only return files, not directories
4352 * @return List of relative Paths to the children
4453 * @throws IOException
4554 * @throws URISyntaxException
4655 */
47- public static List <Path > getListOfChildren ( Path resourceAsPath , boolean filesOnly ) throws IOException , URISyntaxException {
56+ public static List <Path > getListOfChildren (Path resourceAsPath , boolean filesOnly ) throws IOException , URISyntaxException {
4857
49- Path resourcePath = getPathToResource ("/" + resourceAsPath .toString ());
50- int relativeStartIndex = resourcePath .getNameCount ()- resourceAsPath .getNameCount ();
58+ Path resourcePath = getPathToResource ("/" + resourceAsPath .toString ());
59+ int relativeStartIndex = resourcePath .getNameCount () - resourceAsPath .getNameCount ();
5160
5261 final List <Path > result = new ArrayList <>();
5362
54- Files .walk (resourcePath )
55- .filter (p -> !filesOnly || p .toFile ().isFile ())
56- .forEach ( p -> result .add (p .subpath (relativeStartIndex , p .getNameCount ())));
63+ if (resourcePath instanceof ZipPath ) {
64+ try (ZipFile zf = new ZipFile (resourcePath .getFileSystem ().toString ())) {
65+
66+ for (Enumeration list = zf .entries (); list .hasMoreElements (); ) {
67+ ZipEntry entry = (ZipEntry ) list .nextElement ();
68+ // Get entry-path with root element so we can compare it
69+ Path entryPath = resourcePath .getRoot ().resolve (resourcePath .getFileSystem ().getPath (entry .toString ()));
70+
71+ if (entryPath .startsWith (resourcePath ) && (!filesOnly || !entry .isDirectory ()))
72+ result .add (entryPath .subpath (relativeStartIndex , entryPath .getNameCount ()));
73+ }
74+ }
75+ } else {
76+ Files .walk (resourcePath )
77+ .filter (p -> !filesOnly || p .toFile ().isFile ())
78+ .forEach (p -> result .add (p .subpath (relativeStartIndex , p .getNameCount ())));
79+
80+ }
5781
5882 return result ;
5983 }
60-
61- }
84+ }
0 commit comments