How to put files in a ZIP file with NIO.2

URI p = Paths.get( "c:/Users/Christian/Dropbox/jokes.zip" ).toUri();
URI uri = URI.create( "jar:" + p );

Map<String, String> env = new HashMap<>();
env.put( "create", "true" );
try ( FileSystem zipfs = FileSystems.newFileSystem( uri, env ) ) {
  Files.write( zipfs.getPath( "/j1.txt" ), "The truth is out there. Anybody got the URL?".getBytes() );
  Files.write( zipfs.getPath( "/j2.txt" ), "The more I C, the less I see.".getBytes() );
}

Ähnliche Beiträge

Ein Gedanke zu “How to put files in a ZIP file with NIO.2

  1. Using this method, java throws me this exception:

    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.(ZipFileSystem.java:130)
    at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
    at java.nio.file.FileSystems.newFileSystem(Unknown Source)
    at java.nio.file.FileSystems.newFileSystem(Unknown Source)
    at Tests.main(Tests.java:72)

    Any ideas of what may be causing it? Thanks in adavance for the help

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert