是否存在嵌套归档的有效java.net.URI?

时间:2014-11-14 16:54:52

标签: java jar uri archive nio2

尽管可能不明智,但可以读取基本上重命名为 .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方案?

1 个答案:

答案 0 :(得分:1)

如上面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);
    }
}