【发布时间】:2015-01-12 03:26:03
【问题描述】:
虽然可能不明智,但可以读取基本上重命名为 .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 包含嵌套档案时,getFileSystem 和 newFileSystem 调用会失败并返回 IllegalArgumentException,例如当uri 等于jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/some_jar.jar!/ 时(.war 中的 .jar)。
嵌套存档文件是否存在有效的java.net.URI 方案?
【问题讨论】:
-
从记忆中,我会说答案是否定的。 Java 仍然存在围绕在 URI 中转义
!的未修复错误(尝试将 bang 添加到目录名称的末尾,然后将其添加到您的类路径)所以我的直觉反应是说您将有一些工作要做让它按照你想要的方式工作。 -
从java源代码(java.net.JarURLConnection)来看,答案也是否定的:`int separator = spec.indexOf("!/"); /* * 提醒:我们不处理嵌套的 JAR URL */ ...`