JkPathTree implements Closeable { public JkPathTree zipTo(Path destination) { if (destination.getParent() != null) { JkUtilsPath.createDirectories(destination.getParent()); } final Path zipRootEntry = JkUtilsPath.zipRoot(destination); try (Stream<Path> stream = this.stream()) { stream.filter(excludeRootFilter()).forEach(path -> { Path zipEntry = zipRootEntry.resolve(getRoot().relativize(path).toString()); if (!Files.exists(zipEntry) || !Files.isDirectory(zipEntry)) { JkUtilsPath.createDirectories(zipEntry.getParent()); JkUtilsPath.copy(path, zipEntry, StandardCopyOption.REPLACE_EXISTING); } }); zipRootEntry.getFileSystem().close(); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } private  JkPathTree(RootHolder rootHolder, JkPathMatcher matcher); static JkPathTree of(Path rootDir); static JkPathTree ofZip(Path zipFile); Path getRoot(); Path getRootDirOrZipFile(); JkPathMatcher getMatcher(); boolean isDefineMatcher(); boolean exists(); JkPathTree createIfNotExist(); Stream<Path> stream(FileVisitOption ...options); List<Path> getRelativeFiles(); List<Path> getFiles(); JkPathTree goTo(String relativePath); JkPathTree resolvedTo(Path newRoot); Path get(String relativePath); JkPathTree importDir(Path dirToCopy, CopyOption... copyOptions); JkPathTree importTree(JkPathTree tree, CopyOption... copyOptions); JkPathTree importFiles(Iterable<Path> files, StandardCopyOption ... copyOptions); JkPathTree importFile(Path src, String targetName, StandardCopyOption ... copyOptions); JkPathTree deleteContent(); JkPathTree deleteRoot(); JkPathTree zipTo(Path destination); int copyTo(Path destinationDir, CopyOption ... copyOptions); void copyFile(String sourcePath, Path destinationDir, CopyOption ... copyOptions); JkPathTree andMatcher(PathMatcher pathMatcher); JkPathTree withMatcher(JkPathMatcher pathMatcher); JkPathTree andMatching(boolean positive, String... globPatterns); JkPathTree andMatching(String... globPatterns); JkPathTree andMatching(boolean positive, Iterable<String> globPatterns); int count(int max, boolean includeDirectories); JkPathTree resolve(Path path); JkPathTreeSet toSet(); @Override String toString(); @Override void close();  }
@Test public void testZipTo() throws Exception { Path zip = Files.createTempFile("filetree", ".zip"); Files.delete(zip); JkPathTree.of(sampleDir()).zipTo(zip); Path zipRoot = JkUtilsPath.zipRoot(zip); assertTrue(Files.exists(zipRoot.resolve("subfolder/sample.txt"))); zipRoot.getFileSystem().close(); Files.delete(zip); JkPathTree.of(sampleDir()).andMatching(false, "**r/sample.txt").zipTo(zip); zipRoot = JkUtilsPath.zipRoot(zip); assertFalse(Files.exists(zipRoot.resolve("subfolder/sample.txt"))); }