DirectoryZipInputStream extends ZipInputStream { public DirectoryZipInputStream(File sourceDirectory) { super(new ByteArrayInputStream(new byte[0])); this.sourceDirectory = sourceDirectory; this.sourceDirectoryEntries = new ArrayList<File>(FileUtils.listFilesAndDirs(sourceDirectory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)); if (sourceDirectoryEntries.get(0).equals(sourceDirectory)) { sourceDirectoryEntries.remove(0); } this.sourceDirectoryEntriesIterator = sourceDirectoryEntries.iterator(); } DirectoryZipInputStream(File sourceDirectory); File getSourceDirectory(); @Override ZipEntry getNextEntry(); @Override void closeEntry(); @Override int available(); @Override int read(byte[] b, int off, int len); @Override long skip(long n); @Override void close(); @Override int read(); @Override int read(byte[] b); @Override synchronized void mark(int readlimit); @Override synchronized void reset(); @Override boolean markSupported();  }
@Test public void testDirectoryZipInputStream() throws IOException { createDirectoryZip(); File inputStreamFile = File.createTempFile("dirzip-input", null); FileInputStream fileInputStream = new FileInputStream(inputStreamFile); DirectoryZipInputStream directoryZipInputStream = new DirectoryZipInputStream(outputDirectory); ZipEntry zipEntry = null; int directoriesFound = 0; int filesRead = 0; long bytesRead = 0; List<String> entryNamesFound = new ArrayList<String>(); while ((zipEntry = directoryZipInputStream.getNextEntry()) != null) { entryNamesFound.add(zipEntry.getName().replace('\\', '/')); if (zipEntry.isDirectory()) { directoriesFound++; } else { filesRead++; byte[] fileContents = IOUtils.toByteArray(directoryZipInputStream); bytesRead += fileContents.length; } } Assert.assertEquals("Bytes read does not match bytes written", bytesWritten, bytesRead); Assert.assertEquals("Number of files read does not match number of files created", filesCreated, filesRead); Assert.assertEquals("Directories found does not match number of directories created", directoriesCreated, directoriesFound); Assert.assertEquals("Number of entry names does not match !", entryNames.size(), entryNamesFound.size()); Assert.assertTrue("Entry names do not match !", CollectionUtils.isEqualCollection(entryNames, entryNamesFound)); fileInputStream.close(); inputStreamFile.delete(); }