Skip to content

Solution or workaround for nested zip/jar files? #156

@kriegaex

Description

@kriegaex

This test being disabled implies, that you have long been aware of the problem I just had, namely to create a nested zip fille system:

@Test
@Disabled("broken")
void createNestedZips() throws IOException {
FileSystem memoryFileSystem = this.extension.getFileSystem();
Map<String, String> env = Collections.singletonMap("create", "false");
Path outerZip = memoryFileSystem.getPath("/file.zip");
try (OutputStream stream = new JarOutputStream(Files.newOutputStream(outerZip, CREATE_NEW, WRITE))) {
// nothing, just create an empty jar
}
URI uri = URI.create(FS_URI + outerZip.toUri());
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path innerZip = zipfs.getPath("hello.zip");
try (OutputStream stream = new JarOutputStream(Files.newOutputStream(innerZip, CREATE_NEW, WRITE))) {
// nothing, just create an empty jar
}
Map<String, String> env2 = Collections.singletonMap("create", "false");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri2 = URI.create(FS_URI + innerZip.toUri());
try (FileSystem zipfs2 = FileSystems.newFileSystem(uri2, env2)) {
try (BufferedWriter writer = Files.newBufferedWriter(zipfs2.getPath("hello.txt"), US_ASCII, CREATE_NEW, WRITE)) {
writer.write("world");
}
}
}
}

To be fair, the issue occurs identically on JimFS.

Is there any workaround you can recommend to create and read from a nested zip file inside another one on MemoryFS, other than to do it outside of the actual outer zip and copy the file into the zip during creation and caching it outside while reading or modifying it? My use case is testing something related to Spring Boot executable JARs, which contain dependency JARs. What if I want to read some of the inner JARs and verify or update their contents?

java.lang.IllegalArgumentException: URI: jar:memory:__g_1:///home/me/projects/my-project/target/my-project-1.0.jar does not contain path info ex. jar:file:/c:/foo.zip!/BAR

  at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:139)
  at java.base/java.nio.file.Path.of(Path.java:209)
  at java.base/java.nio.file.Paths.get(Paths.java:98)
  at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:76)
  at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:98)
  at java.base/java.nio.file.FileSystems.newFileSystem(FileSystems.java:339)
  ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions