在创建资源路径时从ZipFileSystemProvider获取FileSystemNotFoundException [英] Getting FileSystemNotFoundException from ZipFileSystemProvider when creating a path to a resource
问题描述
我有一个Maven项目,在一个方法中我想为我的资源文件夹中的目录创建一个路径。这是这样做的:
I have a Maven project and inside a method I want to create a path for a directory in my resources folder. This is done like this:
try {
final URI uri = getClass().getResource("/my-folder").toURI();
Path myFolderPath = Paths.get(uri);
} catch (final URISyntaxException e) {
...
}
生成的 URI
看起来像 jar:file:/ C:/path/to/my/project.jar!/ my-folder
。
堆栈跟踪如下:
Exception in thread "pool-4-thread-1" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
URI
似乎有效。 之前的部分!
指向生成的jar文件及其后面的部分到 my-folder
的根部档案。我之前使用过这些说明来创建资源的路径。为什么我现在得到例外?
The URI
seems to be valid. The part before !
points to the generated jar-file and the part after it to my-folder
in the root of the archive. I have used this instructions before to create paths to my resources. Why am I getting an exception now?
推荐答案
您需要创建文件系统才能访问zip中的路径
You need to create the file system before you can access the path within the zip like
final URI uri = getClass().getResource("/my-folder").toURI();
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystem zipfs = FileSystems.newFileSystem(uri, env);
Path myFolderPath = Paths.get(uri);
这不是自动完成的。
这篇关于在创建资源路径时从ZipFileSystemProvider获取FileSystemNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!