Unable to create a zip file using the Zip filesystem provider anymore?
Francis Galiegue
fgaliegue at gmail.com
Fri Apr 29 16:59:24 UTC 2016
Hello,
On Fri, Apr 29, 2016 at 6:33 PM, Xueming Shen <xueming.shen at oracle.com> wrote:
[...]
>> This page disagrees:
>
>
> What do you mean "disagrees"? The note says "create a new zip file if it
> does not exist". In your test
> case the temp file is created via Files.createTempFile(), so for zipfs the
> file exists but it is not in
> zip format.
>
> The JDK7 code looks like (the source is in demo)
>
>
> // configurable env setup
> this.createNew = "true".equals(env.get("create"));
> ...
> this.provider = provider;
> this.zfpath = zfpath;
> if (Files.notExists(zfpath)) {
> if (createNew) {
> try (OutputStream os = Files.newOutputStream(zfpath,
> CREATE_NEW, WRITE)) {
> new END().write(os, 0);
> }
> } else {
> throw new FileSystemNotFoundException(zfpath.toString());
> }
>
> It appears it behaves this way from the very beginning, at least from the
> official release. I did not
> check the history to see if it ever worked way you would like to see in some
> beta releases though :-)
>
I have tried with a fixed path (which I didn't want to do since it
ties you to the underlying OS, but ohwell), and I get the same error:
----
public final class Foo
{
private static final Path ZIP = Paths.get("/tmp/t.zip");
private static final String ENTRY = "foo";
private Foo()
{
throw new Error("instantiation not permitted");
}
public static void main(final String... args)
throws IOException
{
Files.deleteIfExists(ZIP);
Files.createFile(ZIP);
final URI uri = URI.create("jar:" + ZIP.toUri());
try (
final FileSystem fs = FileSystems.newFileSystem(uri,
Collections.singletonMap("create", true));
) {
final Path entry = fs.getPath(ENTRY);
final BasicFileAttributes attrs = Files.readAttributes(entry,
BasicFileAttributes.class);
System.out.println(attrs.creationTime());
}
}
}
----
And I get the same error:
----
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:31)
----
--
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