Skip to content

Instantly share code, notes, and snippets.

@stain
Created May 16, 2013 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stain/5591903 to your computer and use it in GitHub Desktop.
Save stain/5591903 to your computer and use it in GitHub Desktop.
Support for ZIPs with spaces are broken in Java 8b89 Related to http://stackoverflow.com/a/14034572/412540 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7156873 ?
package org.purl.wf4ever.robundle.fs;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipOutputStream;
import org.junit.Test;
/**
* Test http://stackoverflow.com/a/16584723/412540 in Java 8 b89
* @author Stian Soiland-Reyes
*
*/
public class TestZipFS {
@Test
public void jarWithSpaces() throws Exception {
Path dir = Files.createTempDirectory("test");
dir.resolve("test");
Path path = dir.resolve("with several spaces.zip");
// Make empty zip file - the old way!
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(
path, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING))) {
out.closeEntry();
}
Map<String, Object> env = new HashMap<>();
URI root;
// Open by path
try (FileSystem fs = FileSystems.newFileSystem(path, null)) {
// Works fine
root = fs.getPath("/").toUri();
System.out.println(root.toASCIIString());
// Double-escaped, as expected and compatible with Java 7
if (! root.toString().contains("with%2520several%2520spaces.zip")) {
System.out.println("Unexpected");
}
}
// Open it again from the URI
try (FileSystem fs = FileSystems.newFileSystem(root, env)) {
root = fs.getPath("/").toUri();
System.out.println(root.toASCIIString());
if (! (root.toString().contains("with%2520several%2520spaces.zip"))) {
System.out.println("Unexpected");
}
} catch (FileSystemNotFoundException np) {
// Unexpected!
np.printStackTrace();
}
// What if we construct the JAR URI as in Java 7?
URI jar = new URI("jar", path.toUri().toString(), null);
try (FileSystem fs = FileSystems.newFileSystem(jar, env)) {
root = fs.getPath("/").toUri();
System.out.println(root.toASCIIString());
if (! (root.toString().contains("with%2520several%2520spaces.zip"))) {
System.out.println("Unexpected");
}
} catch (FileSystemNotFoundException np) {
// Unexpected!
np.printStackTrace();
}
// OK, let's just create one and see what we get
env.put("create", "true");
Files.delete(path);
try (FileSystem fs = FileSystems.newFileSystem(jar, env)) {
root = fs.getPath("/").toUri();
System.out.println(root.toASCIIString());
// Fails!
//assertTrue(root.toString().contains("with%2520several%2520spaces.zip"));
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file : stream) {
System.out.println(file);
// with%20several%20spaces.zip
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment