1
1
package org .utplsql .api ;
2
2
3
+ import com .sun .nio .zipfs .ZipFileSystem ;
4
+ import com .sun .nio .zipfs .ZipPath ;
3
5
import org .utplsql .api .reporter .CoverageHTMLReporter ;
4
6
7
+ import java .io .File ;
5
8
import java .io .IOException ;
6
9
import java .net .URI ;
7
10
import java .net .URISyntaxException ;
8
11
import java .nio .file .*;
9
12
import java .util .ArrayList ;
10
13
import java .util .Collections ;
14
+ import java .util .Enumeration ;
11
15
import java .util .List ;
16
+ import java .util .zip .ZipEntry ;
17
+ import java .util .zip .ZipFile ;
12
18
13
- /** Helper class for dealing with Resources
19
+ /**
20
+ * Helper class for dealing with Resources
14
21
*
15
22
* @author pesse
16
23
*/
17
24
public class ResourceUtil {
18
25
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
20
28
*
21
29
* @param resourceName The name of the resource
22
30
* @return Path to the resource, either in JAR or on file system
23
31
* @throws IOException
24
32
* @throws URISyntaxException
25
33
*/
26
- public static Path getPathToResource ( String resourceName ) throws IOException , URISyntaxException {
34
+ public static Path getPathToResource (String resourceName ) throws IOException , URISyntaxException {
27
35
URI uri = CoverageHTMLReporter .class .getResource (resourceName ).toURI ();
28
36
Path myPath ;
29
37
if (uri .getScheme ().equalsIgnoreCase ("jar" )) {
@@ -36,26 +44,41 @@ public static Path getPathToResource( String resourceName ) throws IOException,
36
44
return myPath ;
37
45
}
38
46
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.
40
49
*
41
50
* @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
43
52
* @return List of relative Paths to the children
44
53
* @throws IOException
45
54
* @throws URISyntaxException
46
55
*/
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 {
48
57
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 ();
51
60
52
61
final List <Path > result = new ArrayList <>();
53
62
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
+ }
57
81
58
82
return result ;
59
83
}
60
-
61
- }
84
+ }
0 commit comments