### Question:
Zip { public static void extract(InputStream bundle, Path destination) throws IOException { try (ZipInputStream zip = new ZipInputStream(bundle)) { ZipEntry entry = zip.getNextEntry(); if (entry == null) { throw new IllegalStateException("At least one entry expected."); } while (entry != null) { Path entryDestination = entryPath(destination, entry); if (entry.isDirectory()) { Files.createDirectories(entryDestination); } else { Files.copy(zip, entryDestination, StandardCopyOption.REPLACE_EXISTING); } zip.closeEntry(); entry = zip.getNextEntry(); } } } private  Zip(); static void extract(InputStream bundle, Path destination);  }### Answer:
@Test public void test() throws Exception { Path zipFile = tempFolder.newFile().toPath(); try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(zipFile))) { ZipEntry dir = new ZipEntry("dir/"); zout.putNextEntry(dir); zout.closeEntry(); ZipEntry zipEntry = new ZipEntry("dir/file.txt"); zout.putNextEntry(zipEntry); zout.write("Hello World".getBytes()); zout.closeEntry(); } Path out = tempFolder.newDir().toPath(); Zip.extract(Files.newInputStream(zipFile), out); assertThat(out.resolve("dir/file.txt").toFile()).hasContent("Hello World"); }
@Test public void test_empty_zip() throws Exception { Path zipFile = tempFolder.newFile().toPath(); try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(zipFile))) { } Path out = tempFolder.newDir().toPath(); assertThatThrownBy(() -> Zip.extract(Files.newInputStream(zipFile), out)) .isInstanceOf(IllegalStateException.class) .hasMessage("At least one entry expected."); }
@Test public void test_invalid_entry() throws Exception { Path zipFile = tempFolder.newFile().toPath(); try (ZipOutputStream zout = new ZipOutputStream(Files.newOutputStream(zipFile))) { ZipEntry zipEntry = new ZipEntry("../file.txt"); zout.putNextEntry(zipEntry); zout.write("Hello World".getBytes()); zout.closeEntry(); } Path out = tempFolder.newDir().toPath(); assertThatThrownBy(() -> Zip.extract(Files.newInputStream(zipFile), out)) .isInstanceOf(IllegalStateException.class) .hasMessage("Archive entry ../file.txt is not within " + out); }