Unable to create a zip file using the Zip filesystem provider anymore?

Xueming Shen xueming.shen at oracle.com
Fri Apr 29 16:06:32 UTC 2016


I doubt it ever worked this way. The code created a zero-length empty 
file which is not in zip format ...
The "create" is only checked if the file does not exits.

-Sherman

On 4/29/16 8:50 AM, Francis Galiegue wrote:
> 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);
>      }
> }



More information about the nio-dev mailing list