Unable to create a zip file using the Zip filesystem provider anymore?
Francis Galiegue
fgaliegue at gmail.com
Fri Apr 29 15:50:41 UTC 2016
Hello,
Consider this code:
----
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
public final class Foo
{
private static final Path TMPDIR;
static {
final String tmpDir = System.getProperty("java.io.tmpdir");
if (tmpDir == null)
throw new ExceptionInInitializerError("java.io.tmpdir undefined");
TMPDIR = Paths.get(tmpDir);
}
private static final String ZIP_BASE = "whatever";
private static final String ZIP_EXT = ".zip";
private static final String ENTRY = "foo";
private Foo()
{
throw new Error("instantiation not permitted");
}
public static void main(final String... args)
throws IOException
{
final Path zipName = Files.createTempFile(TMPDIR, ZIP_BASE, ZIP_EXT);
final URI uri = URI.create("jar:" + zipName.toUri());
try (
final FileSystem fs = FileSystems.newFileSystem(uri,
Collections.singletonMap("create", "true"));
) {
Files.createFile(fs.getPath(ENTRY));
}
try (
final FileSystem fs = FileSystems.newFileSystem(uri,
Collections.emptyMap());
) {
final Path entry = fs.getPath(ENTRY);
final BasicFileAttributes attrs = Files.readAttributes(entry,
BasicFileAttributes.class);
System.out.println(attrs.creationTime());
}
Files.deleteIfExists(zipName);
}
}
----
I don't know since which version, but now the creation of the
filesystem, and therefore the zip, succeeded... And now I get this:
----
fge at erwin:/tmp$ javac Foo.java
fge at erwin:/tmp$ java Foo
Exception in thread "main" java.util.zip.ZipError: zip END header not found
at com.sun.nio.zipfs.ZipFileSystem.zerror(ZipFileSystem.java:1605)
at com.sun.nio.zipfs.ZipFileSystem.findEND(ZipFileSystem.java:1021)
at com.sun.nio.zipfs.ZipFileSystem.initCEN(ZipFileSystem.java:1030)
at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:130)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
at Foo.main(Foo.java:39)
----
Which means that the "create" attribute of the attribute map is not
obeyed anymore :(
--
Francis Galiegue, fgaliegue at gmail.com, https://github.com/fge
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Parsers in pure Java: https://github.com/fge/grappa
More information about the nio-dev
mailing list