DefaultArchiveExtractor implements ArchiveExtractor { @Override public void extract(File archiveFile, File destinationDirectory) throws ArchiveExtractionException { try { if (archiveFile.getAbsolutePath().endsWith("msi")) { extractMSIArchive(archiveFile, destinationDirectory); } else if (archiveFile.getAbsolutePath().endsWith("zip")) { extractZipArchive(archiveFile, destinationDirectory); } else { extractGzipTarArchive(archiveFile, destinationDirectory); } } catch (IOException e) { throw new ArchiveExtractionException( "Could not extract archive: '" + archiveFile + "'", e); } }  @Override void extract(File archiveFile, File destinationDirectory);  }
@Test public void extractZip_contentsAreExtracted() throws IOException, ArchiveExtractionException { File archiveFile = new File(baseDir, "archive.zip"); archiveFile.createNewFile(); Path tempArchive = archiveFile.toPath(); try (ZipOutputStream zipOutputStream = new ZipOutputStream( Files.newOutputStream(tempArchive))) { zipOutputStream.putNextEntry(new ZipEntry(ROOT_FILE)); zipOutputStream.closeEntry(); zipOutputStream.putNextEntry(new ZipEntry(SUBFOLDER_FILE)); zipOutputStream.closeEntry(); } new DefaultArchiveExtractor().extract(archiveFile, targetDir); Assert.assertTrue("Archive root.file was not extracted", new File(targetDir, ROOT_FILE).exists()); Assert.assertTrue("Archive subfolder/folder.file was not extracted", new File(targetDir, SUBFOLDER_FILE).exists()); }
@Test public void extractTarGz_contentsAreExtracted() throws IOException, ArchiveExtractionException { File archiveFile = new File(baseDir, "archive.tar.gz"); archiveFile.createNewFile(); Path tempArchive = archiveFile.toPath(); try (OutputStream fo = Files.newOutputStream( tempArchive); OutputStream gzo = new GzipCompressorOutputStream( fo); ArchiveOutputStream o = new TarArchiveOutputStream(gzo)) { o.putArchiveEntry( o.createArchiveEntry(new File(ROOT_FILE), ROOT_FILE)); o.closeArchiveEntry(); o.putArchiveEntry( o.createArchiveEntry(new File(SUBFOLDER_FILE), SUBFOLDER_FILE)); o.closeArchiveEntry(); } new DefaultArchiveExtractor().extract(archiveFile, targetDir); Assert.assertTrue("Archive root.file was not extracted", new File(targetDir, ROOT_FILE).exists()); Assert.assertTrue("Archive subfolder/folder.file was not extracted", new File(targetDir, SUBFOLDER_FILE).exists()); }
@Test(expected = ArchiveExtractionException.class) public void extractTarAsZip_ArchiveExtractionExceptionIsThrown() throws IOException, ArchiveExtractionException{ File archiveFile = new File(baseDir, "archive.zip"); archiveFile.createNewFile(); Path tempArchive = archiveFile.toPath(); try (OutputStream fo = Files.newOutputStream( tempArchive); OutputStream gzo = new GzipCompressorOutputStream( fo); ArchiveOutputStream o = new TarArchiveOutputStream(gzo)) { o.putArchiveEntry( o.createArchiveEntry(new File(ROOT_FILE), ROOT_FILE)); o.closeArchiveEntry(); } new DefaultArchiveExtractor().extract(archiveFile, targetDir); }