Skip to content

Commit

Permalink
Allow passing custom parameters when creating Zip FS
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 10, 2024
1 parent 6a307db commit 18994ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/main/java/io/quarkus/fs/util/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,31 @@ public static void zip(Path src, Path zipFile) throws IOException {
}

public static FileSystem newZip(Path zipFile) throws IOException {
final Map<String, Object> 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<String, Object> env) throws IOException {
final Map<String, Object> 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.

// It's OK to not check the existence of the parent dir(s) first, since the API,
// 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);
Expand Down
45 changes: 44 additions & 1 deletion src/test/java/io/quarkus/fs/util/ZipUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 18994ac

Please sign in to comment.