在 Java 中创建 Zip 文件系统提供程序

Creating a Zip File System Provider in Java

来自offical docs,我有这个小程序:

import java.util.*;
import java.io.File;
import java.net.URI;
import java.nio.file.*;

public class ZipFSPUser {
    public static void main(String [] args) throws Throwable {

        Map<String, String> env = new HashMap<>(); 
        env.put("create", "true");

        URI uri = (new File(args[0])).toURI();
        FileSystem fs = FileSystems.newFileSystem(uri, env);

    }
}

我在 Mac 终端中调用它

java -jar app.jar path/to/some/file.zip

但是它说

Path component should be '/'

所以我决定自己写路径:

URI uri = URI.create("file://path/to/my/file.zip");

现在它说

java.lang.IllegalArgumentException: Authority component present

您必须检查 URI syntax

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

hier-part   = "//" authority path-abempty
              / path-absolute
              / path-rootless
              / path-empty

When authority is present, the path must either be empty or begin with a slash ("/") character. When authority is not present, the path cannot begin with two slash characters ("//").

您没有权限。你只是想要一个绝对路径。你的方案也是错误的。根据您发布的 link,您实际上想要 jar:<url>,如 here and here 所述。所以

URI uri = URI.create("jar:file:/path/to/my/file.zip");