@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); }
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(); } } }
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(); } } } }
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(); }
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); }
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);  }