ZipUtils { public static void extract(ZipInputStream zip, File target) throws IOException { try { ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { File file = new File(target, entry.getName()); if (!file.toPath().normalize().startsWith(target.toPath())) { throw new IOException("Bad zip entry"); } if (entry.isDirectory()) { file.mkdirs(); continue; } byte[] buffer = new byte[BUFFER_SIZE]; file.getParentFile().mkdirs(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); int count; while ((count = zip.read(buffer)) != -1) { out.write(buffer, 0, count); } out.close(); } } finally { zip.close(); } } private  ZipUtils(); static void extract(ZipInputStream zip, File target);  }
@Test void extract() throws IOException { File outputDirectory = directoryPath.toFile(); BufferedInputStream stream = new BufferedInputStream(ZipUtils.class.getResourceAsStream("/commons/zip-utils-test.zip")); ZipInputStream zipStream = new ZipInputStream(stream); ZipUtils.extract(zipStream, outputDirectory); File directory = new File(outputDirectory, "directory"); Assertions.assertTrue(directory.exists()); Node<File> map = FileUtils.collectFiles(directory); Assertions.assertEquals("directory", map.getElement().getName()); Set<String> files = map.collectLeafs(File::isFile).stream() .map(File::getName) .collect(Collectors.toSet()); Assertions.assertEquals(Sets.newHashSet("1.txt", "2.txt"), files); }