@Test public void testExtractZipFile() throws Exception { final File tempDirectory = FileUtils.getTempDirectory(); final File unzippedFolder = new File(tempDirectory,"testData"); try { if (unzippedFolder.exists()) { unzippedFolder.delete(); } unzippedFolder.mkdir(); final File zipFile = new File(getClass().getResource("/SnomedCT_Release_INT_20140131.zip").toURI()); ZipFileUtils.extractFilesFromZipToOneFolder(zipFile, unzippedFolder.getAbsolutePath()); final File[] filesUnzipped = unzippedFolder.listFiles(); assertEquals(filesUnzipped.length, 3); for( final File file : filesUnzipped) { assertTrue(file.isFile()); } } finally { FileUtils.deleteQuietly(unzippedFolder); } }
public static void extractZipFile(final File file, final String outputDir) throws IOException { try (ZipFile zipFile = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final File entryDestination = new File(outputDir, entry.getName()); entryDestination.getParentFile().mkdirs(); if (!entryDestination.toPath().normalize().startsWith(outputDir)) { LOGGER.error("Bad zip entry " + entry.getName()); continue; } if (entry.isDirectory()) { entryDestination.mkdirs(); } else { InputStream in = null; OutputStream out = null; try { in = zipFile.getInputStream(entry); out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } }
ZipFileUtils { public static void extractZipFile(final File file, final String outputDir) throws IOException { try (ZipFile zipFile = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final File entryDestination = new File(outputDir, entry.getName()); entryDestination.getParentFile().mkdirs(); if (!entryDestination.toPath().normalize().startsWith(outputDir)) { LOGGER.error("Bad zip entry " + entry.getName()); continue; } if (entry.isDirectory()) { entryDestination.mkdirs(); } else { InputStream in = null; OutputStream out = null; try { in = zipFile.getInputStream(entry); out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } } }
ZipFileUtils { public static void extractZipFile(final File file, final String outputDir) throws IOException { try (ZipFile zipFile = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final File entryDestination = new File(outputDir, entry.getName()); entryDestination.getParentFile().mkdirs(); if (!entryDestination.toPath().normalize().startsWith(outputDir)) { LOGGER.error("Bad zip entry " + entry.getName()); continue; } if (entry.isDirectory()) { entryDestination.mkdirs(); } else { InputStream in = null; OutputStream out = null; try { in = zipFile.getInputStream(entry); out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } }  }
ZipFileUtils { public static void extractZipFile(final File file, final String outputDir) throws IOException { try (ZipFile zipFile = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final File entryDestination = new File(outputDir, entry.getName()); entryDestination.getParentFile().mkdirs(); if (!entryDestination.toPath().normalize().startsWith(outputDir)) { LOGGER.error("Bad zip entry " + entry.getName()); continue; } if (entry.isDirectory()) { entryDestination.mkdirs(); } else { InputStream in = null; OutputStream out = null; try { in = zipFile.getInputStream(entry); out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } }  static void extractFilesFromZipToOneFolder(final File file, final String outputDir); static void extractZipFile(final File file, final String outputDir); static void zip(final String sourceFileDir, final String zipFile); static void unzip(String zipFilePath, String destDirectory); }
ZipFileUtils { public static void extractZipFile(final File file, final String outputDir) throws IOException { try (ZipFile zipFile = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final File entryDestination = new File(outputDir, entry.getName()); entryDestination.getParentFile().mkdirs(); if (!entryDestination.toPath().normalize().startsWith(outputDir)) { LOGGER.error("Bad zip entry " + entry.getName()); continue; } if (entry.isDirectory()) { entryDestination.mkdirs(); } else { InputStream in = null; OutputStream out = null; try { in = zipFile.getInputStream(entry); out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } } }  static void extractFilesFromZipToOneFolder(final File file, final String outputDir); static void extractZipFile(final File file, final String outputDir); static void zip(final String sourceFileDir, final String zipFile); static void unzip(String zipFilePath, String destDirectory);  }