Using NIO2 for writing zip file entries without corresponding folder entries

Alan Bateman Alan.Bateman at oracle.com
Tue Oct 16 14:21:57 UTC 2018


On 16/10/2018 14:00, Rafael Winterhalter wrote:
> :
>
>         // Emulate merging two zip files without directories using NIO2
>         try (
>             FileSystem source = FileSystems.newFileSystem(sample, null);
>             FileSystem target = FileSystems.newFileSystem(other, null)
>         ) {
>             Path path = source.getPath("foo/bar.txt");
>             Files.copy(path, target.getPath(source.toString()));
>         }
>
> It surprised me that this does not work when trying to migrate from 
> ZipInputStream to NIO2.
If I read this correctly, it is attempting to copy foo/bar.txt from one 
file system to a non-existent directory in the other file system. As I 
said, the zip file system tries to behave like a regular file system so 
it is correct to fail when the directory doesn't exist. I think the only 
thing we can do here is introduce a configuration option to strip 
directories when writing, it's just something that hasn't come up before.

-Alan



More information about the nio-dev mailing list