FileExtractor { public String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException { DownloadableFileType fileType = DownloadableFileType.valueOf(FilenameUtils.getExtension(downloadedCompressedFile.getName()).toUpperCase()); LOG.debug("Determined archive type: " + fileType); LOG.debug("Overwrite files that exist: " + overwriteFilesThatExist); switch (fileType) { case GZ: case BZ2: CompressedFile compressedFile = new CompressedFile(downloadedCompressedFile); if (null != compressedFile.getArchiveType() && compressedFile.getArchiveType().equals(TAR)) { return untarFile(compressedFile.getInputStream(), extractedToFilePath, possibleFilenames); } else { return copyFileToDisk(compressedFile.getInputStream(), extractedToFilePath, compressedFile.getDecompressedFilename()); } case ZIP: return unzipFile(downloadedCompressedFile, extractedToFilePath, possibleFilenames); case EXE: if (possibleFilenames.getBinaryFilenames().contains(downloadedCompressedFile.getName())) { return copyFileToDisk(new FileInputStream(downloadedCompressedFile), extractedToFilePath, downloadedCompressedFile.getName()); } default: throw new IllegalArgumentException("." + fileType + " is an unsupported archive type"); } } FileExtractor(boolean overwriteFilesThatExist); String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames);  }
@Test public void overwriteExistingFileWhenExtractingFromZip() throws Exception { FileExtractor fileExtractor = new FileExtractor(OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); fileExtractor.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(not(equalTo(lastModified)))); }
@Test public void doNotOverwriteExistingFileWhenExtractingFromZip() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); fileExtractor.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(equalTo(lastModified))); }
@Test public void successfullyExtractFileFromTarGZipArchive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); String extractedFilePath = fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH))); assertThat(extractedFilePath, is(equalTo(tempDir + File.separator + "phantomjs"))); }
@Test public void overwriteExistingFileWhenExtractingFromTar() throws Exception { FileExtractor fileExtractor = new FileExtractor(OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(not(equalTo(lastModified)))); }
@Test public void doNotOverwriteExistingFileWhenExtractingFromTar() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(equalTo(lastModified))); }
@Test public void successfullyExtractFileFromTarBZip2Archive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testTarBZ2File.getFile()), tempDir, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH))); }
@Test(expected = IllegalArgumentException.class) public void tryToUntarAnArchiveThatIsNotATarFile() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(test7ZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); }
@Test public void successfullyWorkOutArchiveTypeAndExtractFileFromZipArchive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH))); }
@Test public void successfullyWorkOutArchiveTypeAndExtractFileFromTarGZipArchive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH))); }
@Test public void successfullyWorkOutArchiveTypeAndExtractFileFromTarBZip2Archive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(testTarBZ2File.getFile()), tempDir, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH))); }
@Test(expected = IllegalArgumentException.class) public void tryAndExtractFromAnUnsupportedArchive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); fileExtractor.extractFileFromArchive(new File(test7ZipFile.getFile()), tempDir, BinaryType.PHANTOMJS); }
@Test public void successfullyDownloadAnEXEFile() throws Exception { FileExtractor fileExtractor = new FileExtractor(OVERWRITE_EXISTING_FILES); String extractedFile = fileExtractor.extractFileFromArchive(new File(textExeFile.getFile()), tempDir, BinaryType.EDGE); FileInputStream fileToCheck = new FileInputStream(extractedFile); String downloadedFileHash = DigestUtils.sha1Hex(fileToCheck); fileToCheck.close(); assertThat(downloadedFileHash, is(equalTo(VALID_HASH_FOR_EXE))); }
@Test public void successfullyDownloadAnFirefoxZipFile() throws Exception { FileExtractor fileExtractor = new FileExtractor(OVERWRITE_EXISTING_FILES); String extractedFile = fileExtractor.extractFileFromArchive(new File(testFirefoxZipFile.getFile()), tempDir, BinaryType.FIREFOX); assertThat(extractedFile, containsString("firefox")); }
@Test public void successfullyDownloadAnFirefoxTarFile() throws Exception { FileExtractor fileExtractor = new FileExtractor(OVERWRITE_EXISTING_FILES); String extractedFile = fileExtractor.extractFileFromArchive(new File(testFirefoxTarFile.getFile()), tempDir, BinaryType.FIREFOX); assertThat(extractedFile, containsString("firefox")); }