是否存在嵌套存档的有效java.net.uri?
通过使用是否存在嵌套存档的有效java.net.uri?,java,jar,uri,archive,nio2,Java,Jar,Uri,Archive,Nio2,通过使用jar:,可以读取基本上重命名为.zip文件(.ear、.war、.jar等)的归档格式,尽管这可能是不明智的 例如,当uri变量的计算结果为单个顶级存档时,例如当uri等于jar时,以下代码运行良好:file:///Users/justingarrick/Desktop/test/my_war.war!/ private FileSystem createZipFileSystem(Path path) throws IOException { URI uri = URI.cr
jar:
,可以读取基本上重命名为.zip文件(.ear、.war、.jar等)的归档格式,尽管这可能是不明智的
例如,当uri
变量的计算结果为单个顶级存档时,例如当uri
等于jar时,以下代码运行良好:file:///Users/justingarrick/Desktop/test/my_war.war!/代码>
private FileSystem createZipFileSystem(Path path) throws IOException {
URI uri = URI.create("jar:" + path.toUri().toString());
FileSystem fs;
try {
fs = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
fs = FileSystems.newFileSystem(uri, new HashMap<>());
}
return fs;
}
专用文件系统createZipFileSystem(路径路径)引发IOException{
URI=URI.create(“jar:+path.toUri().toString());
文件系统fs;
试一试{
fs=FileSystems.getFileSystem(uri);
}捕获(FileSystemNotFounde异常){
fs=FileSystems.newFileSystem(uri,newHashMap());
}
返回fs;
}
但是,当URI包含嵌套存档时,getFileSystem
和newFileSystem
调用会失败,并出现IllegalArgumentException
,例如当URI
等于jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/一些罐子/代码>(一个.war中的.jar)
嵌套存档文件是否有有效的java.net.URI
方案?正如Jonas Berlin在上面的评论中所述,答案是否。从:
从内存来看,答案是否定的。Java在转义方面仍然存在未修复的错误代码>在URI中(尝试在目录名的末尾添加一个bang,然后将其添加到类路径中),因此我的直觉反应是,要让它以您想要的方式工作,您需要做一些工作。从java源代码(java.net.JarURLConnection)中,答案也是否定的:`int separator=spec.indexOf(!/“”); /* * 提醒:我们不处理嵌套的JAR URL*/`
/* get the specs for a given url out of the cache, and compute and
* cache them if they're not there.
*/
private void parseSpecs(URL url) throws MalformedURLException {
String spec = url.getFile();
int separator = spec.indexOf("!/");
/*
* REMIND: we don't handle nested JAR URLs
*/
if (separator == -1) {
throw new MalformedURLException("no !/ found in url spec:" + spec);
}
jarFileURL = new URL(spec.substring(0, separator++));
entryName = null;
/* if ! is the last letter of the innerURL, entryName is null */
if (++separator != spec.length()) {
entryName = spec.substring(separator, spec.length());
entryName = ParseUtil.decode (entryName);
}
}