Skip to content

Commit

Permalink
Handle paths with a '?' in them
Browse files Browse the repository at this point in the history
This is actually quite tricky, as URI does not let you create these by
default, you need to sneak them in using URL.toUrl(). If you attempt to
use the URI constructor directly then a '?' is interpreted as the start
of the query string, and %3f is encoded to %253f, so there is no way to
actually pass in an ecoded question mark to the URI constructor.
  • Loading branch information
stuartwdouglas committed Feb 15, 2022
1 parent 81c6411 commit ed472ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/io/quarkus/fs/util/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static void unzip(Path zipFile, Path targetDir) throws IOException {
public static URI toZipUri(Path zipFile) throws IOException {
URI zipUri = zipFile.toUri();
try {
zipUri = new URI(JAR_URI_PREFIX + zipUri.getScheme(), zipUri.getPath(), null);
zipUri = new URL(JAR_URI_PREFIX + zipUri.getScheme() + "://" + zipUri.getRawPath() + "!/").toURI();
} catch (URISyntaxException e) {
throw new IOException("Failed to create a JAR URI for " + zipFile, e);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/io/quarkus/fs/util/ZipUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

class ZipUtilsTest {

Expand Down Expand Up @@ -74,6 +76,32 @@ public void testNewZip() throws Exception {
}
}

/**
* Test that the {@link ZipUtils#newZip(Path)} works as expected when the path contains a question mark
*
* Windows does not support question marks in file names
*
* @throws Exception
*/
@Test
@DisabledOnOs(OS.WINDOWS)
public void testNewZipWithQuestionMark() throws Exception {
final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
final Path zipPath = Paths.get(tmpDir.toString(), "ziputils?test-" + System.currentTimeMillis() + ".jar");
try {
try (final FileSystem fs = ZipUtils.newZip(zipPath)) {
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)) {
assertFileExistsWithContent(fs.getPath("hello.txt"), "hello");
}
} finally {
Files.deleteIfExists(zipPath);
}
}

/**
* Tests that the {@link ZipUtils#newZip(Path)} works correctly, and creates the zip file,
* when the parent directories of the {@link Path} passed to it are not present.
Expand Down

0 comments on commit ed472ac

Please sign in to comment.