From 18994ac9df143feb6de3495a0b34049a370d2820 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 10 Jan 2024 18:57:33 +0200 Subject: [PATCH] Allow passing custom parameters when creating Zip FS Relates to: https://github.com/quarkusio/quarkus/issues/38128 --- .../java/io/quarkus/fs/util/ZipUtils.java | 19 ++++++-- .../java/io/quarkus/fs/util/ZipUtilsTest.java | 45 ++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/quarkus/fs/util/ZipUtils.java b/src/main/java/io/quarkus/fs/util/ZipUtils.java index 8cfcb59..24fd7a2 100644 --- a/src/main/java/io/quarkus/fs/util/ZipUtils.java +++ b/src/main/java/io/quarkus/fs/util/ZipUtils.java @@ -114,11 +114,19 @@ public static void zip(Path src, Path zipFile) throws IOException { } public static FileSystem newZip(Path zipFile) throws IOException { - final Map env; + return newZip(zipFile, Collections.emptyMap()); + } + + /** + * In this version, callers can set whatever parameters they like for the FileSystem. + * Note that these values always take precedence over whatever the library itself sets. + */ + public static FileSystem newZip(Path zipFile, Map env) throws IOException { + final Map effectiveEnv; if (Files.exists(zipFile)) { - env = DEFAULT_OWNER_ENV; + effectiveEnv = new HashMap<>(DEFAULT_OWNER_ENV); } else { - env = CREATE_ENV; + effectiveEnv = new HashMap<>(CREATE_ENV); // explicitly create any parent dirs, since the ZipFileSystem only creates a new file // with "create" = "true", but doesn't create any parent dirs. @@ -126,8 +134,11 @@ public static FileSystem newZip(Path zipFile) throws IOException { // as per its contract doesn't throw any exception if the parent dir(s) already exist Files.createDirectories(zipFile.getParent()); } + if (env != null) { + effectiveEnv.putAll(env); + } try { - return FileSystemProviders.ZIP_PROVIDER.newFileSystem(toZipUri(zipFile), env); + return FileSystemProviders.ZIP_PROVIDER.newFileSystem(toZipUri(zipFile), effectiveEnv); } catch (IOException ioe) { // include the URI for which the filesystem creation failed throw new IOException("Failed to create a new filesystem for " + zipFile, ioe); diff --git a/src/test/java/io/quarkus/fs/util/ZipUtilsTest.java b/src/test/java/io/quarkus/fs/util/ZipUtilsTest.java index 0ff1b22..cf4cc50 100644 --- a/src/test/java/io/quarkus/fs/util/ZipUtilsTest.java +++ b/src/test/java/io/quarkus/fs/util/ZipUtilsTest.java @@ -1,7 +1,7 @@ package io.quarkus.fs.util; import static org.junit.jupiter.api.Assertions.assertEquals; - +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; @@ -10,6 +10,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; @@ -76,6 +77,48 @@ public void testNewZip() throws Exception { } } + /** + * Test that the {@link ZipUtils#newZip(Path, Map)} works as expected + * + * @throws Exception + */ + @Test + public void testNewNonCompressedZip() throws Exception { + final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")); + final Path zipPath = Paths.get(tmpDir.toString(), "ziputilstest-" + System.currentTimeMillis() + ".jar"); + try { + try (final FileSystem fs = ZipUtils.newZip(zipPath, Map.of("compressionMethod", "STORED"))) { + final Path someFileInZip = fs.getPath("hello.txt"); + Files.write(someFileInZip, "hello".getBytes(StandardCharsets.UTF_8)); + } + // now just verify that the content was actually written out + try (final FileSystem fs = ZipUtils.newFileSystem(zipPath)) { + Path helloFilePath = fs.getPath("hello.txt"); + assertFileExistsWithContent(helloFilePath, "hello"); + } + + + } finally { + Files.deleteIfExists(zipPath); + } + } + + @Test + public void tesIllegalEnv() { + assertThrows(IllegalArgumentException.class, () -> { + final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")); + final Path zipPath = Paths.get(tmpDir.toString(), "ziputilstest-" + System.currentTimeMillis() + ".jar"); + try { + try (final FileSystem fs = ZipUtils.newZip(zipPath, Map.of("compressionMethod", "DUMMY"))) { + final Path someFileInZip = fs.getPath("hello.txt"); + Files.write(someFileInZip, "hello".getBytes(StandardCharsets.UTF_8)); + } + } finally { + Files.deleteIfExists(zipPath); + } + }); + } + /** * Test that the {@link ZipUtils#newZip(Path)} works as expected when the path contains a question mark *