-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve zip support for IOUtils (#7653)
This patch moves IOUtils zip handling to use `FileSystems.newFileSystem(Path, ...)` API instead of the URI-based method. The URI-based FS is shared globally, so it can lead to race conditions when opening/closing file systems. The Path-based FS is unshared and can be used safely. Additionally, this patch implements support for nested jar files. For Java 11+ this works with default zipfs, but earlier versions do not support zipfs nesting, so there's a fallback to extract the intermediate jar instead. Resolves #7620 Resolves #7626
- Loading branch information
Showing
2 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
core/src/test/groovy/io/micronaut/core/io/IOUtilsSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.micronaut.core.io | ||
|
||
import spock.lang.Specification | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipOutputStream | ||
|
||
class IOUtilsSpec extends Specification { | ||
def 'nested access to same zip file'() { | ||
given: | ||
Path zipPath = Files.createTempFile("micronaut-ioutils-spec", ".zip") | ||
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipPath))) { | ||
zos.putNextEntry(new ZipEntry("foo/bar.txt")) | ||
zos.write("baz".getBytes(StandardCharsets.UTF_8)) | ||
zos.closeEntry() | ||
} | ||
|
||
def visitedOuter = [] | ||
def visitedInner = [] | ||
|
||
when: | ||
IOUtils.eachFile(URI.create('jar:' + zipPath.toUri()), 'foo', entry -> { | ||
visitedOuter.add(entry.getFileName().toString()) | ||
IOUtils.eachFile(URI.create('jar:' + zipPath.toUri()), 'foo', entryI -> { | ||
visitedInner.add(entryI.getFileName().toString()) | ||
}) | ||
}) | ||
then: | ||
visitedOuter == ['bar.txt'] | ||
visitedInner == ['bar.txt'] | ||
|
||
cleanup: | ||
Files.deleteIfExists(zipPath) | ||
} | ||
|
||
def 'access to nested zip files'() { | ||
given: | ||
Path zipPath = Files.createTempFile("micronaut-ioutils-spec", ".zip") | ||
try (ZipOutputStream outer = new ZipOutputStream(Files.newOutputStream(zipPath))) { | ||
outer.putNextEntry(new ZipEntry("foo/inner.zip")) | ||
|
||
ZipOutputStream inner = new ZipOutputStream(outer) | ||
inner.putNextEntry(new ZipEntry("bar/baz.txt")) | ||
inner.write("bla".getBytes(StandardCharsets.UTF_8)) | ||
inner.closeEntry() | ||
inner.finish() | ||
|
||
outer.closeEntry() | ||
} | ||
|
||
def visitedInner = [] | ||
def textInner = [] | ||
|
||
when: | ||
IOUtils.eachFile(URI.create('jar:' + zipPath.toUri() + '!/foo/inner.zip!/xyz'), 'bar', entry -> { | ||
visitedInner.add(entry.getFileName().toString()) | ||
textInner = Files.readAllLines(entry) | ||
}) | ||
then: | ||
visitedInner == ['baz.txt'] | ||
textInner == ['bla'] | ||
|
||
cleanup: | ||
Files.deleteIfExists(zipPath) | ||
} | ||
} |