@Test void extractFile_andTargetFolderExistsWithExistingFile_successfulAndExistingFilesAreRemoved() throws IOException, ZipFileExtractionException { File extractedFolder = new File(targetFolder, "aFolder"); File existingFileInTargetFolder = new File(extractedFolder, "existingFile.txt"); givenTargetFolderWithExistingFile(extractedFolder, existingFileInTargetFolder); ZipFileExtractor.extractFile(zipFile, targetFolder); assertAllFoldersAndFilesAreExtracted(extractedFolder); assertZipFileStillExists(zipFile); assertFalse(existingFileInTargetFolder.exists()); }
public static void extractFile(final File zipFileToExtract, final File targetDir) throws ZipFileExtractionException { ZipFile zipFile = getZipFile(zipFileToExtract); if (targetDir.exists()) { try { FileUtils.deleteDirectory(targetDir); } catch (IOException e) { throw new ZipFileExtractionException("Could not delete target directory.", e); } } try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(targetDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.close(); } } } catch (IOException e) { throw new ZipFileExtractionException("IO Error while extracting ZIP file.", e); } finally { IOUtils.closeQuietly(zipFile); } }
ZipFileExtractor { public static void extractFile(final File zipFileToExtract, final File targetDir) throws ZipFileExtractionException { ZipFile zipFile = getZipFile(zipFileToExtract); if (targetDir.exists()) { try { FileUtils.deleteDirectory(targetDir); } catch (IOException e) { throw new ZipFileExtractionException("Could not delete target directory.", e); } } try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(targetDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.close(); } } } catch (IOException e) { throw new ZipFileExtractionException("IO Error while extracting ZIP file.", e); } finally { IOUtils.closeQuietly(zipFile); } } }
ZipFileExtractor { public static void extractFile(final File zipFileToExtract, final File targetDir) throws ZipFileExtractionException { ZipFile zipFile = getZipFile(zipFileToExtract); if (targetDir.exists()) { try { FileUtils.deleteDirectory(targetDir); } catch (IOException e) { throw new ZipFileExtractionException("Could not delete target directory.", e); } } try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(targetDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.close(); } } } catch (IOException e) { throw new ZipFileExtractionException("IO Error while extracting ZIP file.", e); } finally { IOUtils.closeQuietly(zipFile); } }  }
ZipFileExtractor { public static void extractFile(final File zipFileToExtract, final File targetDir) throws ZipFileExtractionException { ZipFile zipFile = getZipFile(zipFileToExtract); if (targetDir.exists()) { try { FileUtils.deleteDirectory(targetDir); } catch (IOException e) { throw new ZipFileExtractionException("Could not delete target directory.", e); } } try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(targetDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.close(); } } } catch (IOException e) { throw new ZipFileExtractionException("IO Error while extracting ZIP file.", e); } finally { IOUtils.closeQuietly(zipFile); } }  static void extractFile(final File zipFileToExtract, final File targetDir); }
ZipFileExtractor { public static void extractFile(final File zipFileToExtract, final File targetDir) throws ZipFileExtractionException { ZipFile zipFile = getZipFile(zipFileToExtract); if (targetDir.exists()) { try { FileUtils.deleteDirectory(targetDir); } catch (IOException e) { throw new ZipFileExtractionException("Could not delete target directory.", e); } } try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(targetDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); out.close(); } } } catch (IOException e) { throw new ZipFileExtractionException("IO Error while extracting ZIP file.", e); } finally { IOUtils.closeQuietly(zipFile); } }  static void extractFile(final File zipFileToExtract, final File targetDir);  }