Zip FS Writes Archive Entries

Mount a ZIP via the zipfs provider with a jar: URI and create=true, create parent directories explicitly, then read and write entries with the Files API.

JavaFile Upload

Code

snippet
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

public class ArchiveAsFileSystem {

    public static void main(String[] args) throws Exception {
        Path zip = Path.of("build-config.zip");
        Files.deleteIfExists(zip);

        // jar: URI + create=true:文件不存在则新建,存在则打开
        Map<String, String> env = Map.of("create", "true");
        try (FileSystem zipfs = FileSystems.newFileSystem(
                URI.create("jar:" + zip.toUri()), env)) {
            Path root = zipfs.getPath("/");

            // zipfs 不会自动补建父目录:newOutputStream 在 CREATE 路径上会先
            // checkParents(),父目录条目不存在直接抛 NoSuchFileException。
            // 所以写文件前必须先 createDirectories(这一句同时建出 config
            // 和 config/profiles 两级)。
            Files.createDirectories(root.resolve("config/profiles"));
            Files.writeString(root.resolve("config/app.properties"),
                    "retries=3\ntimeoutMs=1500\n");
            Files.writeString(root.resolve("config/profiles/prod.properties"),
                    "retries=5\n");
            Files.writeString(root.resolve("README.txt"), "generated\n");

            try (var entries = Files.walk(zipfs.getPath("/"))) {
                entries.filter(Files::isRegularFile)
                       .forEach(p -> {
                           try {
                               System.out.println(p + " (" + Files.size(p) + " bytes)");
                           } catch (IOException e) {
                               throw new UncheckedIOException(e);
                           }
                       });
            }
        }

        // 关闭文件系统即提交归档:之后的读取走真实 zip 文件
        try (FileSystem reopened = FileSystems.newFileSystem(zip, Map.of())) {
            System.out.println("reopened: "
                    + Files.readString(reopened.getPath("/config/app.properties")).lines()
                            .findFirst().orElse(""));
        }
    }
}

Inside the snippet

Under the hood: the class ArchiveAsFileSystem.

Resources are declared in a try-with-resources header, so they close on every exit path without a finally block. Invalid states are rejected up front: it throws UncheckedIOException instead of continuing with bad data.

Watch out for

  • Copy, run: the standard library answers every call in the file.

Putting it to use

The file announces its steps in the output: reopened:.

Core lines

throw new UncheckedIOException(e);

FAQ

How are precondition violations signaled?
The code refuses to continue: `UncheckedIOException` is thrown while the state is still recoverable.
What does Zip FS Writes Archive Entries print when run?
Execute the whole file; each printed line is prefixed with a label like `reopened:`.