-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResourceUtil.java
More file actions
67 lines (59 loc) · 2.38 KB
/
ResourceUtil.java
File metadata and controls
67 lines (59 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.utplsql.api;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
/**
* Helper class for dealing with Resources
*
* @author pesse
*/
public class ResourceUtil {
private ResourceUtil() {
}
/**
* Copy directory from a jar file to the destination folder
*
* @param resourceAsPath The resource to get children from
* @param targetDirectory If set to true it will only return files, not directories
*/
public static void copyResources(Path resourceAsPath, Path targetDirectory) {
String resourceName = "/" + resourceAsPath.toString();
try {
Files.createDirectories(targetDirectory);
URI uri = ResourceUtil.class.getResource(resourceName).toURI();
Path myPath;
if (uri.getScheme().equalsIgnoreCase("jar")) {
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
myPath = fileSystem.getPath(resourceName);
copyRecursive(myPath, targetDirectory);
}
} else {
myPath = Paths.get(uri);
copyRecursive(myPath, targetDirectory);
}
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
private static void copyRecursive(Path from, Path targetDirectory) throws IOException {
Files.walkFileTree(from, new SimpleFileVisitor<Path>() {
private Path currentTarget;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
super.preVisitDirectory(dir, attrs);
currentTarget = targetDirectory.resolve(from.relativize(dir).toString());
Files.createDirectories(currentTarget);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
super.visitFile(file, attrs);
Files.copy(file, targetDirectory.resolve(from.relativize(file).toString()), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}