11package org .utplsql .api ;
22
3- import com .sun .nio .zipfs .ZipPath ;
4- import org .utplsql .api .reporter .CoverageHTMLReporter ;
5-
63import java .io .IOException ;
74import java .net .URI ;
85import java .net .URISyntaxException ;
96import java .nio .file .*;
10- import java .util . ArrayList ;
7+ import java .nio . file . attribute . BasicFileAttributes ;
118import java .util .Collections ;
12- import java .util .Enumeration ;
13- import java .util .List ;
14- import java .util .zip .ZipEntry ;
15- import java .util .zip .ZipFile ;
169
1710/**
1811 * Helper class for dealing with Resources
@@ -25,63 +18,50 @@ private ResourceUtil() {
2518 }
2619
2720 /**
28- * Returns the Path to a resource so it is walkable no matter if it's inside a jar or on the file system
21+ * Copy directory from a jar file to the destination folder
2922 *
30- * @param resourceName The name of the resource
31- * @return Path to the resource, either in JAR or on file system
32- * @throws IOException
33- * @throws URISyntaxException
23+ * @param resourceAsPath The resource to get children from
24+ * @param targetDirectory If set to true it will only return files, not directories
3425 */
35- public static Path getPathToResource (String resourceName ) throws IOException , URISyntaxException {
36- URI uri = CoverageHTMLReporter .class .getResource (resourceName ).toURI ();
37- Path myPath ;
38- if (uri .getScheme ().equalsIgnoreCase ("jar" )) {
39- FileSystem fileSystem = FileSystems .newFileSystem (uri , Collections .emptyMap ());
40- myPath = fileSystem .getPath (resourceName );
41- } else {
42- myPath = Paths .get (uri );
26+ public static void copyResources (Path resourceAsPath , Path targetDirectory ) {
27+ String resourceName = "/" + resourceAsPath .toString ();
28+ try {
29+ Files .createDirectories (targetDirectory );
30+ URI uri = ResourceUtil .class .getResource (resourceName ).toURI ();
31+ Path myPath ;
32+ if (uri .getScheme ().equalsIgnoreCase ("jar" )) {
33+ try (FileSystem fileSystem = FileSystems .newFileSystem (uri , Collections .emptyMap ())) {
34+ myPath = fileSystem .getPath (resourceName );
35+ copyRecursive (myPath , targetDirectory );
36+ }
37+ } else {
38+ myPath = Paths .get (uri );
39+ copyRecursive (myPath , targetDirectory );
40+ }
41+ } catch (IOException | URISyntaxException e ) {
42+ throw new RuntimeException (e );
4343 }
44-
45- return myPath ;
4644 }
4745
48- /**
49- * Returns the relative paths of all children of the given resource. Relative path begins from the first atom of the given path.
50- *
51- * @param resourceAsPath The resource to get children from
52- * @param filesOnly If set to true it will only return files, not directories
53- * @return List of relative Paths to the children
54- * @throws IOException
55- * @throws URISyntaxException
56- */
57- public static List <Path > getListOfChildren (Path resourceAsPath , boolean filesOnly ) throws IOException , URISyntaxException {
58-
59- Path resourcePath = getPathToResource ("/" + resourceAsPath .toString ());
60- int relativeStartIndex = resourcePath .getNameCount () - resourceAsPath .getNameCount ();
61-
62- final List <Path > result = new ArrayList <>();
46+ private static void copyRecursive (Path from , Path targetDirectory ) throws IOException {
47+ Files .walkFileTree (from , new SimpleFileVisitor <Path >() {
6348
64- if (resourcePath instanceof ZipPath ) {
65- try (ZipFile zf = new ZipFile (resourcePath .getFileSystem ().toString ())) {
49+ private Path currentTarget ;
6650
67- for (Enumeration list = zf .entries (); list .hasMoreElements (); ) {
68- ZipEntry entry = (ZipEntry ) list .nextElement ();
69- // Get entry-path with root element so we can compare it
70- Path entryPath = resourcePath .getRoot ().resolve (resourcePath .getFileSystem ().getPath (entry .toString ()));
71-
72- if (entryPath .startsWith (resourcePath ) && (!filesOnly || !entry .isDirectory ())) {
73- result .add (entryPath .subpath (relativeStartIndex , entryPath .getNameCount ()));
74- }
75- }
51+ @ Override
52+ public FileVisitResult preVisitDirectory (Path dir , BasicFileAttributes attrs ) throws IOException {
53+ super .preVisitDirectory (dir , attrs );
54+ currentTarget = targetDirectory .resolve (from .relativize (dir ).toString ());
55+ Files .createDirectories (currentTarget );
56+ return FileVisitResult .CONTINUE ;
7657 }
77- resourcePath .getFileSystem ().close ();
78- } else {
79- Files .walk (resourcePath )
80- .filter (p -> !filesOnly || p .toFile ().isFile ())
81- .forEach (p -> result .add (p .subpath (relativeStartIndex , p .getNameCount ())));
8258
83- }
84-
85- return result ;
59+ @ Override
60+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
61+ super .visitFile (file , attrs );
62+ Files .copy (file , targetDirectory .resolve (from .relativize (file ).toString ()), StandardCopyOption .REPLACE_EXISTING );
63+ return FileVisitResult .CONTINUE ;
64+ }
65+ });
8666 }
8767}
0 commit comments