【问题标题】:Do valid java.net.URIs for nested archives exist?嵌套档案的有效 java.net.URI 是否存在?
【发布时间】: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 包含嵌套档案时,getFileSystemnewFileSystem 调用会失败并返回 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 */ ...`

标签: java jar uri archive nio2


【解决方案1】:

正如上面乔纳斯柏林的评论所述,答案是。来自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);
    }
}

【讨论】:

    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多