I am creating a zip file from the contents of a source directory using NIO2. I am using a ZipFileSystem, for which I first have to gain an instance, and then generate paths. The generated paths can then be used to create entries in the zip file using Files.createDirectory(pathInZip)
or Files.copy(sourcePath, destPathInZip)
. This works fine, but there is a moment of uglyness that I would like to avoid:
// within the SimpleFileVisitor that walks through sourceDirFile
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path pathInZip = zipFileSystem.getPath(sourceDirPath.relativize(file).toString()); // <-- ?!
Files.copy(file, pathInZip);
return FileVisitResult.CONTINUE;
}
Is there a way to copy a Path from one FileSystemProvider into a Path from another without relying on aPath.toString()
?. It seems ugly. I could always iterate over one path, incrementally building the other... but it would seem so easy to have a FileSystem.getPath(Path anotherPath) that I took the time to write this post.