ZipUtilities { @VisibleForTesting public static String zipDir(String srcDir, String dstFile) throws IOException { FileOutputStream fos = new FileOutputStream(dstFile); ZipOutputStream zos = new ZipOutputStream(fos); File srcFile = new File(srcDir); LOG.info("Compressing directory {}", srcDir); addDirToZip(zos, srcFile, srcFile); zos.close(); LOG.info("Compressed directory {} to file: {}", srcDir, dstFile); return dstFile; } private  ZipUtilities(); @VisibleForTesting static String zipDir(String srcDir, String dstFile);  }
@Test public void testZipEmptyDir() throws IOException { File dstFile = getDestinationFile(); ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath()); assertCountOfZipEntries(dstFile, 0); }
@Test public void testZipDirWithOneFile() throws IOException { fileUtils.createFileInDir(tempDir, "test1"); File dstFile = getDestinationFile(); ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath()); assertCountOfZipEntries(dstFile, 1); }
@Test public void testZipDirWithMultipleFiles() throws IOException { fileUtils.createFileInDir(tempDir, "test1"); fileUtils.createFileInDir(tempDir, "test2"); fileUtils.createFileInDir(tempDir, "test3"); File dstFile = getDestinationFile(); ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath()); assertCountOfZipEntries(dstFile, 3); }
@Test public void testZipDirComplex() throws IOException { fileUtils.createFileInDir(tempDir, "test1"); fileUtils.createFileInDir(tempDir, "test2"); fileUtils.createFileInDir(tempDir, "test3"); File subdir1 = fileUtils.createDirectory(tempDir, "subdir1"); File subdir2 = fileUtils.createDirectory(tempDir, "subdir2"); fileUtils.createFileInDir(subdir1, "file1_1"); fileUtils.createFileInDir(subdir1, "file1_2"); fileUtils.createFileInDir(subdir2, "file2_1"); fileUtils.createFileInDir(subdir2, "file2_2"); File dstFile = getDestinationFile(); ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath()); assertZipEntriesByName(dstFile, Sets.newHashSet( "test1", "test2", "test3", "subdir1/file1_1", "subdir1/file1_2", "subdir2/file2_1", "subdir2/file2_2" )); }