@Test public void successfullyExtractFileFromZipArchive() throws Exception { FileExtractor fileExtractor = new FileExtractor(DO_NOT_OVERWRITE_EXISTING_FILES); String extractedFilePath = fileExtractor.unzipFile(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))); assertThat(extractedFilePath, is(equalTo(tempDir + File.separator + "phantomjs"))); }
String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList<String> filenamesWeAreSearchingFor = new ArrayList<String>(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); try { Enumeration<ZipArchiveEntry> zipFile = zip.getEntries(); if (filenamesWeAreSearchingFor.get(0).equals("*")) { filenamesWeAreSearchingFor.remove(0); LOG.debug("Extracting full archive"); return this.unzipFolder(zip, extractedToFilePath, filenamesWeAreSearchingFor); } else { while (zipFile.hasMoreElements()) { ZipArchiveEntry zipFileEntry = zipFile.nextElement(); for (String aFilenameWeAreSearchingFor : filenamesWeAreSearchingFor) { if (zipFileEntry.getName().endsWith(aFilenameWeAreSearchingFor)) { LOG.debug("Found: " + zipFileEntry.getName()); return copyFileToDisk(zip.getInputStream(zipFileEntry), extractedToFilePath, aFilenameWeAreSearchingFor); } } } } } finally { zip.close(); } throw new ExpectedFileNotFoundException("Unable to find any expected files for " + possibleFilenames.getBinaryTypeAsString()); }
FileExtractor { String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList<String> filenamesWeAreSearchingFor = new ArrayList<String>(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); try { Enumeration<ZipArchiveEntry> zipFile = zip.getEntries(); if (filenamesWeAreSearchingFor.get(0).equals("*")) { filenamesWeAreSearchingFor.remove(0); LOG.debug("Extracting full archive"); return this.unzipFolder(zip, extractedToFilePath, filenamesWeAreSearchingFor); } else { while (zipFile.hasMoreElements()) { ZipArchiveEntry zipFileEntry = zipFile.nextElement(); for (String aFilenameWeAreSearchingFor : filenamesWeAreSearchingFor) { if (zipFileEntry.getName().endsWith(aFilenameWeAreSearchingFor)) { LOG.debug("Found: " + zipFileEntry.getName()); return copyFileToDisk(zip.getInputStream(zipFileEntry), extractedToFilePath, aFilenameWeAreSearchingFor); } } } } } finally { zip.close(); } throw new ExpectedFileNotFoundException("Unable to find any expected files for " + possibleFilenames.getBinaryTypeAsString()); } }
FileExtractor { String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList<String> filenamesWeAreSearchingFor = new ArrayList<String>(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); try { Enumeration<ZipArchiveEntry> zipFile = zip.getEntries(); if (filenamesWeAreSearchingFor.get(0).equals("*")) { filenamesWeAreSearchingFor.remove(0); LOG.debug("Extracting full archive"); return this.unzipFolder(zip, extractedToFilePath, filenamesWeAreSearchingFor); } else { while (zipFile.hasMoreElements()) { ZipArchiveEntry zipFileEntry = zipFile.nextElement(); for (String aFilenameWeAreSearchingFor : filenamesWeAreSearchingFor) { if (zipFileEntry.getName().endsWith(aFilenameWeAreSearchingFor)) { LOG.debug("Found: " + zipFileEntry.getName()); return copyFileToDisk(zip.getInputStream(zipFileEntry), extractedToFilePath, aFilenameWeAreSearchingFor); } } } } } finally { zip.close(); } throw new ExpectedFileNotFoundException("Unable to find any expected files for " + possibleFilenames.getBinaryTypeAsString()); } FileExtractor(boolean overwriteFilesThatExist); }
FileExtractor { String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList<String> filenamesWeAreSearchingFor = new ArrayList<String>(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); try { Enumeration<ZipArchiveEntry> zipFile = zip.getEntries(); if (filenamesWeAreSearchingFor.get(0).equals("*")) { filenamesWeAreSearchingFor.remove(0); LOG.debug("Extracting full archive"); return this.unzipFolder(zip, extractedToFilePath, filenamesWeAreSearchingFor); } else { while (zipFile.hasMoreElements()) { ZipArchiveEntry zipFileEntry = zipFile.nextElement(); for (String aFilenameWeAreSearchingFor : filenamesWeAreSearchingFor) { if (zipFileEntry.getName().endsWith(aFilenameWeAreSearchingFor)) { LOG.debug("Found: " + zipFileEntry.getName()); return copyFileToDisk(zip.getInputStream(zipFileEntry), extractedToFilePath, aFilenameWeAreSearchingFor); } } } } } finally { zip.close(); } throw new ExpectedFileNotFoundException("Unable to find any expected files for " + possibleFilenames.getBinaryTypeAsString()); } FileExtractor(boolean overwriteFilesThatExist); String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames); }
FileExtractor { String unzipFile(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames) throws IOException, ExpectedFileNotFoundException { LOG.debug("Attempting to extract binary from .zip file..."); ArrayList<String> filenamesWeAreSearchingFor = new ArrayList<String>(possibleFilenames.getBinaryFilenames()); ZipFile zip = new ZipFile(downloadedCompressedFile); try { Enumeration<ZipArchiveEntry> zipFile = zip.getEntries(); if (filenamesWeAreSearchingFor.get(0).equals("*")) { filenamesWeAreSearchingFor.remove(0); LOG.debug("Extracting full archive"); return this.unzipFolder(zip, extractedToFilePath, filenamesWeAreSearchingFor); } else { while (zipFile.hasMoreElements()) { ZipArchiveEntry zipFileEntry = zipFile.nextElement(); for (String aFilenameWeAreSearchingFor : filenamesWeAreSearchingFor) { if (zipFileEntry.getName().endsWith(aFilenameWeAreSearchingFor)) { LOG.debug("Found: " + zipFileEntry.getName()); return copyFileToDisk(zip.getInputStream(zipFileEntry), extractedToFilePath, aFilenameWeAreSearchingFor); } } } } } finally { zip.close(); } throw new ExpectedFileNotFoundException("Unable to find any expected files for " + possibleFilenames.getBinaryTypeAsString()); } FileExtractor(boolean overwriteFilesThatExist); String extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, BinaryType possibleFilenames);  }