gpt4 book ai didi

java - 是否存在用于嵌套存档的有效 java.net.URI?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:36:01 24 4
gpt4 key购买 nike

虽然可能不明智,但有可能读取基本上重命名为 .zip 文件的存档格式(.ear.war , .jar 等),通过使用 jar: URI scheme .

例如,当 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;
}

但是,当 URI 包含嵌套存档时,getFileSystemnewFileSystem 调用会失败并出现 IllegalArgumentException,例如当 uri 等于 jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/some_jar.jar!/(。 jar.war 中)。

是否有用于嵌套存档文件的有效 java.net.URI 方案?

最佳答案

正如 Jonas Berlin 在上面的评论中所述,答案是。来自java.net.JarURLConnection source :

/* 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);
}
}

关于java - 是否存在用于嵌套存档的有效 java.net.URI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com