@Test public void testUnzipEmptyFile() throws IOException { File empty = _folder.newFile("empty"); try (FileOutputStream fos = new FileOutputStream(empty); ZipOutputStream ignored = new ZipOutputStream(fos)) { assert ignored != null; } File dest = _folder.newFolder("dest"); UnzipUtility.unzip(empty.toPath(), dest.toPath()); assertThat("output folder exists", dest.exists(), is(true)); assertThat("output folder is a directory", dest.isDirectory(), is(true)); assertThat("output folder is empty", dest.list(), equalTo(new String[0])); }
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);  }