Skip to content

Commit 7ad370b

Browse files
committed
Fix problems when copying resources from JAR
1 parent 2c9bc81 commit 7ad370b

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
package org.utplsql.api;
22

3+
import com.sun.nio.zipfs.ZipFileSystem;
4+
import com.sun.nio.zipfs.ZipPath;
35
import org.utplsql.api.reporter.CoverageHTMLReporter;
46

7+
import java.io.File;
58
import java.io.IOException;
69
import java.net.URI;
710
import java.net.URISyntaxException;
811
import java.nio.file.*;
912
import java.util.ArrayList;
1013
import java.util.Collections;
14+
import java.util.Enumeration;
1115
import 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
*/
1724
public 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+
}

src/main/java/org/utplsql/api/reporter/CoverageHTMLReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void writeSQL(SQLOutput stream) throws SQLException {
7979
private static void copyFileFromClasspath( Path assetPath, Path targetDirectory, int filterNumOfFolders ) throws IOException {
8080

8181
Path assetStartPath = assetPath.subpath(filterNumOfFolders, assetPath.getNameCount());
82-
Path targetAssetPath = targetDirectory.resolve(assetStartPath);
82+
Path targetAssetPath = targetDirectory.resolve(Paths.get(assetStartPath.toString()));
8383

8484
Files.createDirectories(targetAssetPath.getParent());
8585

0 commit comments

Comments
 (0)