Zipper implements AutoCloseable { public void addDirectoryToZip(final Path sourceDirectory, final PathPredicate directoryPredicate, final FilePredicate filePredicate, final String destinationDirectoryName) throws IOException { walkFileTree(sourceDirectory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (!directoryPredicate.accept(dir)) { return FileVisitResult.SKIP_SUBTREE; } return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (filePredicate.accept(file.toFile())) { addToZip(file, normalizeZipEntryName(get(destinationDirectoryName).resolve(sourceDirectory.relativize(file)))); } return FileVisitResult.CONTINUE; } }); } Zipper(OutputStream destStream); void addToZip(byte[] bytes, String destFilename); void addToZip(Path path, String destFilename); void addDirectoryToZip(final Path sourceDirectory,
                                  final PathPredicate directoryPredicate,
                                  final FilePredicate filePredicate,
                                  final String destinationDirectoryName); void close(); static final PathPredicate ALL_DIRECTORIES; static final FilePredicate ALL_FILES; }
@Test public void should_zip_a_directory() throws Exception { zipper.addDirectoryToZip(get(dir), ALL_DIRECTORIES, ALL_FILES, ""); Path dest = unzip(out); expectSameDirContent(dest, get(dir)); }
@Test public void should_zip_a_directory_in_a_destination_folder_without_filter() throws Exception { zipper.addDirectoryToZip(get(dir), ALL_DIRECTORIES, ALL_FILES, "destinationInZip"); Path dest = unzip(out); expectSameDirContent(dest.resolve("destinationInZip"), get(dir)); }
@Test public void should_zip_a_directory_in_a_destination_folder() throws Exception { zipper.addDirectoryToZip(get(dir), new IncludeChildDirectoryPredicate(get(dir), singleton("aSubDirectory")), ALL_FILES, "destinationInZip"); Path dest = unzip(out); expectSameDirContent(dest.resolve("destinationInZip"), get(dir)); }
@Test public void should_zipentry_contains_paths_instead_of_file_separators() throws Exception { zipper.addDirectoryToZip(get(dir), new IncludeChildDirectoryPredicate(get(dir), singleton("aSubDirectory")), ALL_FILES, "destinationInZip"); Set<String> entries = zipEntries(out); assertThat(entries).contains("destinationInZip/aFile.txt"); }
@Test public void should_zip_a_directory_and_filter_out_unaccepted_subdirectories() throws Exception { String destinationInZip = "destinationInZip"; zipper.addDirectoryToZip(get(dir), new IncludeChildDirectoryPredicate(get(dir), singleton("nonExisting")), ALL_FILES, destinationInZip); Path dest = unzip(out); assertThat(dest.resolve(destinationInZip).toFile().list()).containsExactly("aFile.txt"); }
@Test public void should_zip_a_directory_and_filter_out_unaccepted_descriptorjsonfile() throws Exception { String destinationInZip = "destinationInZip"; zipper.addDirectoryToZip(get(getClass().getResource("/workspace/widgets/pbLabel").toURI()), ALL_DIRECTORIES, new ExcludeDescriptorFilePredicate("pbLabel.json"), destinationInZip); Path dest = unzip(out); assertThat(dest.resolve(destinationInZip).toFile().list()).doesNotContain("pbLabel.json"); }