ZipUtil { public static void zipDirectory(File directory, File zipFile) throws IOException { @SuppressWarnings("unchecked") Collection<File> files = FileUtils.listFiles(directory, null, true); FileOutputStream fzos = null; ZipOutputStream zos = null; try { fzos = new FileOutputStream(zipFile); zos = new ZipOutputStream(fzos); for(File f : files) { String fileNameInZIP = directory.toURI().relativize(f.toURI()).getPath(); ZipEntry zipEntry = new ZipEntry(fileNameInZIP); zos.putNextEntry(zipEntry); FileInputStream fileInputStream = new FileInputStream(f); try { IOUtils.copy(fileInputStream, zos); } finally { IOUtils.closeQuietly(fileInputStream); } } } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(fzos); } } private  ZipUtil(); static void unzipFile(File zip, File dir); static void zipDirectory(File directory, File zipFile);  }
@Test public void testZipDirectory() throws Exception { File zipFile = createZipForTesting(); List<String> expectedEntries = new ArrayList<String>(); expectedEntries.add("a.txt"); expectedEntries.add("b.txt"); expectedEntries.add("subdir/a.txt"); ZipFile f = new ZipFile(zipFile); try { Enumeration<? extends ZipEntry> entries = f.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); assertTrue(expectedEntries.contains(entry.getName())); expectedEntries.remove(entry.getName()); } assertEquals(0, expectedEntries.size()); } finally { f.close(); } }