通过使用jar: URI scheme,虽然可能不明智,但有可能读取基本上已重命名为.zip文件(.ear,.war,.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包含嵌套的归档文件(例如URI)时,getFileSystemnewFileSystem调用会失败,并显示IllegalArgumentException。当uri等于jar:jar:file:///Users/justingarrick/Desktop/test/my_war.war!/some_jar.jar!/(.war内的.jar)时。

嵌套存档文件是否有有效的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/

10-11 04:03
查看更多