Illustration einer tropischen Insel mit Palmen und einem hohen Berg

Java Blog für Programmierer

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

· Christian Ullenboom

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

Map 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() );
}

Kategorien: Java 7, Snippet

1 Kommentare

Ana Zerpa ·

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

Die bisherigen Kommentare wurden aus WordPress übernommen. Neue Kommentare sind derzeit geschlossen.

← Alle Beiträge