### Question:
ZipUtils { public static void zip( FileSystemAbstraction fileSystem, File sourceToCompress, File destinationZip ) throws IOException { if ( !fileSystem.fileExists( sourceToCompress ) ) { return; } if ( isEmptyDirectory( fileSystem, sourceToCompress ) ) { return; } Map<String,String> env = MapUtil.stringMap( "create", "true" ); Path rootPath = sourceToCompress.toPath(); URI archiveAbsoluteURI = URI.create( "jar:file:" + destinationZip.toURI().getRawPath() ); try ( FileSystem zipFs = FileSystems.newFileSystem( archiveAbsoluteURI, env ) ) { List<FileHandle> fileHandles = fileSystem.streamFilesRecursive( sourceToCompress ).collect( toList() ); for ( FileHandle fileHandle : fileHandles ) { Path sourcePath = fileHandle.getFile().toPath(); Path zipFsPath = fileSystem.isDirectory( sourceToCompress ) ? zipFs.getPath( rootPath.relativize( sourcePath ).toString() ) : zipFs.getPath( sourcePath.getFileName().toString() ); if ( zipFsPath.getParent() != null ) { Files.createDirectories( zipFsPath.getParent() ); } Files.copy( sourcePath, zipFsPath ); } } }  static void zip( FileSystemAbstraction fileSystem, File sourceToCompress, File destinationZip );  }### Answer:
@Test void doNotCreateZipArchiveForNonExistentSource() throws IOException { File archiveFile = testDirectory.file( "archive.zip" ); ZipUtils.zip( fileSystem, testDirectory.file( "doesNotExist" ), archiveFile ); assertFalse( fileSystem.fileExists( archiveFile ) ); }
@Test void doNotCreateZipArchiveForEmptyDirectory() throws IOException { File archiveFile = testDirectory.file( "archive.zip" ); File emptyDirectory = testDirectory.directory( "emptyDirectory" ); ZipUtils.zip( fileSystem, emptyDirectory, archiveFile ); assertFalse( fileSystem.fileExists( archiveFile ) ); }
@Test void archiveDirectory() throws IOException { File archiveFile = testDirectory.file( "directoryArchive.zip" ); File directory = testDirectory.directory( "directory" ); fileSystem.create( new File( directory, "a" ) ).close(); fileSystem.create( new File( directory, "b" ) ).close(); ZipUtils.zip( fileSystem, directory, archiveFile ); assertTrue( fileSystem.fileExists( archiveFile ) ); assertEquals( 2, countArchiveEntries( archiveFile ) ); }
@Test void archiveDirectoryWithSubdirectories() throws IOException { File archiveFile = testDirectory.file( "directoryWithSubdirectoriesArchive.zip" ); File directoryArchive = testDirectory.directory( "directoryWithSubdirs" ); File subdir1 = new File( directoryArchive, "subdir1" ); File subdir2 = new File( directoryArchive, "subdir" ); fileSystem.mkdir( subdir1 ); fileSystem.mkdir( subdir2 ); fileSystem.create( new File( directoryArchive, "a" ) ).close(); fileSystem.create( new File( directoryArchive, "b" ) ).close(); fileSystem.create( new File( subdir1, "c" ) ).close(); fileSystem.create( new File( subdir2, "d" ) ).close(); ZipUtils.zip( fileSystem, directoryArchive, archiveFile ); assertTrue( fileSystem.fileExists( archiveFile ) ); assertEquals( 6, countArchiveEntries( archiveFile ) ); }
@Test void archiveFile() throws IOException { File archiveFile = testDirectory.file( "fileArchive.zip" ); File aFile = testDirectory.file( "a" ); fileSystem.create( aFile ).close(); ZipUtils.zip( fileSystem, aFile, archiveFile ); assertTrue( fileSystem.fileExists( archiveFile ) ); assertEquals( 1, countArchiveEntries( archiveFile ) ); }
@Test public void supportSpacesInDestinationPath() throws IOException { File archiveFile = testDirectory.file( "file archive.zip" ); File aFile = testDirectory.file( "a" ); fileSystem.create( aFile ).close(); ZipUtils.zip( fileSystem, aFile, archiveFile ); }