ZipFiles { public static void zipDirectory(File sourceDirectory, File zipFile) throws IOException { checkNotNull(sourceDirectory); checkNotNull(zipFile); checkArgument( sourceDirectory.isDirectory(), "%s is not a valid directory", sourceDirectory.getAbsolutePath()); checkArgument( !zipFile.exists(), "%s does already exist, files are not being overwritten", zipFile.getAbsolutePath()); Closer closer = Closer.create(); try { OutputStream outputStream = closer.register(new BufferedOutputStream(new FileOutputStream(zipFile))); zipDirectory(sourceDirectory, outputStream); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } private  ZipFiles(); static void zipDirectory(File sourceDirectory, File zipFile); static void zipDirectory(File sourceDirectory, OutputStream outputStream);  }
@Test public void testEmptySubdirectoryHasZipEntry() throws Exception { File zipDir = new File(tmpDir, "zip"); File subDirEmpty = new File(zipDir, "subDirEmpty"); assertTrue(subDirEmpty.mkdirs()); ZipFiles.zipDirectory(tmpDir, zipFile); assertZipOnlyContains("zip/subDirEmpty/"); }
@Test public void testSubdirectoryWithContentsHasNoZipEntry() throws Exception { File zipDir = new File(tmpDir, "zip"); File subDirContent = new File(zipDir, "subdirContent"); assertTrue(subDirContent.mkdirs()); createFileWithContents(subDirContent, "myTextFile.txt", "Simple Text"); ZipFiles.zipDirectory(tmpDir, zipFile); assertZipOnlyContains("zip/subdirContent/myTextFile.txt"); }