@Test public void testUnzipEntriesOutOfOrder() throws IOException { byte[] contents = "contents of file.txt".getBytes(); File notOrdered = _folder.newFile("not_ordered"); try (FileOutputStream fos = new FileOutputStream(notOrdered); ZipOutputStream out = new ZipOutputStream(fos)) { out.putNextEntry(new ZipEntry("dir/file.txt")); out.write(contents); out.putNextEntry(new ZipEntry("dir/")); } File dest = _folder.newFolder("dest"); UnzipUtility.unzip(notOrdered.toPath(), dest.toPath()); }
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);  }