[英]How to construct a Path from a jar:file URL?
How do I construct a Path
to a jar:file
URL?如何构造jar:file
URL 的Path
?
Invoking Paths.get(new URI("jar:file:/C:/foo.jar!/bar.html"))
throws FileSystemNotFoundException
(notice the file system is missing, not the file itself).调用Paths.get(new URI("jar:file:/C:/foo.jar!/bar.html"))
抛出FileSystemNotFoundException
(注意文件系统丢失,而不是文件本身)。
As far as I can tell, both files exist.据我所知,这两个文件都存在。 Any ideas?有任何想法吗?
Paths
tries to resolve a FileSystem
which would contain your Path
. Paths
尝试解析包含您的Path
的FileSystem
。 (Actually this may be an implementation detail. The spec simply states that it will check the default FileSystem
.) If you haven't registered/created such a FileSystem
, it won't be able to find it. (实际上这可能是一个实现细节。规范只是说明它将检查默认的FileSystem
。)如果您还没有注册/创建这样的FileSystem
,它将无法找到它。
You would create a new FileSystem
from the jar file and access the entry Path
through that FileSystem
.您将从 jar 文件创建一个新的FileSystem
并通过该FileSystem
访问条目Path
。
Path path = Paths.get("C:/foo.jar");
URI uri = new URI("jar", path.toUri().toString(), null);
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystem fileSystem = FileSystems.newFileSystem(uri, env);
Path file = fileSystem.getPath("bar.html");
System.out.println(file);
You could then use然后你可以使用
Paths.get(new URI("jar:file:/C:/foo.jar!/bar.html"))
Be careful to properly close the FileSystem
when finished using it.要小心,以正确关闭FileSystem
使用它完成时。
For more information about ZipFileSystemProvider, see here .有关 ZipFileSystemProvider 的更多信息,请参见此处。
Starting with version 7, Java allows us to have FileSystems, not only open a file on the local directory, but to also define our own File System.从版本 7 开始,Java 允许我们拥有文件系统,不仅可以在本地目录上打开文件,还可以定义我们自己的文件系统。 There are many uses for it, such as having a distributed file system, being able to use compression, have an http bridge, so many things...它有很多用途,例如拥有分布式文件系统、能够使用压缩、拥有 http 桥接器等等……
In your case, what you need is a way to read a jar.在您的情况下,您需要的是一种读取 jar 的方法。 Well, since a jar is just a zipped file, you can use the FileSystem with the default.好吧,由于 jar 只是一个压缩文件,因此您可以使用默认的 FileSystem。 It doesn't get any easier than that:没有比这更容易的了:
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String [] args) throws Throwable {
String jarName = "/Users/asantos/.m2/repository/ant/ant/1.6/ant-1.6.jar";
String fileInside = "/META-INF/LICENSE.txt";
Map<String, String> env = new HashMap<>();
URI uri = URI.create("jar:file:"+jarName);
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
System.out.println(new String(Files.readAllBytes(zipfs.getPath(fileInside))));
}
}
}
I shamelessly copied my code from the documentation, adapting for a jar instead of a zip file: http://docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/zipfilesystemprovider.html我无耻地从文档中复制了我的代码,适用于 jar 而不是 zip 文件: http : //docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/zipfilesystemprovider.html
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.*;
public static void method() {
Map<String, String> env = new HashMap<>();
// here false is used to indicate the zip file system provider not to create a new zip/jar file if it does not exist.
env.put("create", Boolean.FALSE.toString());
Path jarFilePath = Paths.get("C:\\test_jar_parent", "myjarfile.jar");
String uriPath = "jar:" + jarFilePath.toUri().toString();
URI uri = URI.create(uriPath);
try (FileSystem jarFs = FileSystems.newFileSystem(uri, env, null)) {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.