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.
I have written some utility methods for this. Maybe you find them useful (the library is Open Source).
Tutorial: http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html
Javadoc: http://softsmithy.sourceforge.net/lib/current/docs/api/softsmithy-lib-core/index.html
Maven:
More info: http://puces-blog.blogspot.ch/2012/07/news-from-software-smithy-version-02.html