7

使用URI scheme可以读取基本上重命名为.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;
}

但是,当 URI 包含嵌套档案时, getFileSystemandnewFileSystem调用会失败IllegalArgumentException,例如当uriequals jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/some_jar.jar!/.war内的.jar)时。

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

4

1 回答 1

1

正如上面乔纳斯柏林的评论所述,答案是否定的。从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);
    }
}
于 2014-12-15T17:14:17.870 回答