@Test public void testUnzipThrowsOnMkdirErrors() { File dest; File test_zip; try { test_zip = _folder.newFile("test_zip"); try (FileOutputStream fos = new FileOutputStream(test_zip); ZipOutputStream out = new ZipOutputStream(fos)) { out.putNextEntry(new ZipEntry("dir/")); } dest = _folder.newFolder("dest"); dest.setWritable(false); } catch (IOException e) { throw new AssertionError("test failed with IOException for the wrong reason"); } try { UnzipUtility.unzip(test_zip.toPath(), dest.toPath()); fail(); } catch (IOException e) { assertThat(e.getMessage(), containsString("Unable to make directory")); } }
public static void unzip(InputStream zipStream, Path destDirectory) throws IOException { if (!destDirectory.toFile().isDirectory()) { throw new IOException( String.format( "Output directory does not exist or is not a direcotry: %s", destDirectory)); } try (ZipInputStream zipIn = new ZipInputStream(zipStream)) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { Path outputPath = validatePath( new File(destDirectory.toFile(), new File(entry.getName()).getPath()).toPath(), destDirectory); if (entry.isDirectory()) { if (!outputPath.toFile().exists()) { if (!outputPath.toFile().mkdirs()) { throw new IOException("Unable to make directory " + outputPath); } } } else { createParentDirectories(outputPath); extractFile(zipIn, outputPath); } zipIn.closeEntry(); } } }
UnzipUtility { public static void unzip(InputStream zipStream, Path destDirectory) throws IOException { if (!destDirectory.toFile().isDirectory()) { throw new IOException( String.format( "Output directory does not exist or is not a direcotry: %s", destDirectory)); } try (ZipInputStream zipIn = new ZipInputStream(zipStream)) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { Path outputPath = validatePath( new File(destDirectory.toFile(), new File(entry.getName()).getPath()).toPath(), destDirectory); if (entry.isDirectory()) { if (!outputPath.toFile().exists()) { if (!outputPath.toFile().mkdirs()) { throw new IOException("Unable to make directory " + outputPath); } } } else { createParentDirectories(outputPath); extractFile(zipIn, outputPath); } zipIn.closeEntry(); } } } }
UnzipUtility { public static void unzip(InputStream zipStream, Path destDirectory) throws IOException { if (!destDirectory.toFile().isDirectory()) { throw new IOException( String.format( "Output directory does not exist or is not a direcotry: %s", destDirectory)); } try (ZipInputStream zipIn = new ZipInputStream(zipStream)) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { Path outputPath = validatePath( new File(destDirectory.toFile(), new File(entry.getName()).getPath()).toPath(), destDirectory); if (entry.isDirectory()) { if (!outputPath.toFile().exists()) { if (!outputPath.toFile().mkdirs()) { throw new IOException("Unable to make directory " + outputPath); } } } else { createParentDirectories(outputPath); extractFile(zipIn, outputPath); } zipIn.closeEntry(); } } } private  UnzipUtility(); }
UnzipUtility { public static void unzip(InputStream zipStream, Path destDirectory) throws IOException { if (!destDirectory.toFile().isDirectory()) { throw new IOException( String.format( "Output directory does not exist or is not a direcotry: %s", destDirectory)); } try (ZipInputStream zipIn = new ZipInputStream(zipStream)) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { Path outputPath = validatePath( new File(destDirectory.toFile(), new File(entry.getName()).getPath()).toPath(), destDirectory); if (entry.isDirectory()) { if (!outputPath.toFile().exists()) { if (!outputPath.toFile().mkdirs()) { throw new IOException("Unable to make directory " + outputPath); } } } else { createParentDirectories(outputPath); extractFile(zipIn, outputPath); } zipIn.closeEntry(); } } } private  UnzipUtility(); static void unzip(InputStream zipStream, Path destDirectory); static void unzip(Path zipFile, Path destDirectory); }
UnzipUtility { public static void unzip(InputStream zipStream, Path destDirectory) throws IOException { if (!destDirectory.toFile().isDirectory()) { throw new IOException( String.format( "Output directory does not exist or is not a direcotry: %s", destDirectory)); } try (ZipInputStream zipIn = new ZipInputStream(zipStream)) { for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) { Path outputPath = validatePath( new File(destDirectory.toFile(), new File(entry.getName()).getPath()).toPath(), destDirectory); if (entry.isDirectory()) { if (!outputPath.toFile().exists()) { if (!outputPath.toFile().mkdirs()) { throw new IOException("Unable to make directory " + outputPath); } } } else { createParentDirectories(outputPath); extractFile(zipIn, outputPath); } zipIn.closeEntry(); } } } private  UnzipUtility(); static void unzip(InputStream zipStream, Path destDirectory); static void unzip(Path zipFile, Path destDirectory);  }