Jus*_*ick 7 java uri jar archive nio2
虽然可能是不明智的,但是通过使用URI方案来读取基本上重命名的.zip文件(.ear,.war,.jar等)的存档格式是可能的.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;
}
Run Code Online (Sandbox Code Playgroud)
然而,getFileSystem
和newFileSystem
调用失败的IllegalArgumentException
时候URI包含嵌套压缩文件,例如,当uri
平等jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/some_jar.jar!/
(一的.jar一个内部的.war).
java.net.URI
嵌套存档文件是否有有效的方案?
正如乔纳斯·柏林上面的评论所述,答案是否定的。来自java.net.JarURLConnection 源:
/* 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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
405 次 |
最近记录: |