public
Created

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 ?

  • Download Gist
TestZipFS.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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
}
}
}
 
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.