LOG.info(String.format("Unzipping %s to dir %s.", inputFile
.getAbsolutePath(), outputDir.getAbsolutePath()));
final List<File> unzippedFiles = new LinkedList<File>();
final InputStream is = new FileInputStream(inputFile);
final ZipArchiveInputStream debInputStream = (ZipArchiveInputStream) new ArchiveStreamFactory()
.createArchiveInputStream("zip", is);
ZipArchiveEntry entry = null;
while ((entry = (ZipArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
LOG.info(String.format(
"Attempting to write output directory %s.", outputFile
.getAbsolutePath()));
if (!outputFile.exists()) {
LOG.info(String.format(
"Attempting to create output directory %s.",
outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format(
"Couldn't create directory %s.", outputFile
.getAbsolutePath()));
}
}
} else {
LOG.info(String.format("Creating output file %s.", outputFile
.getAbsolutePath()));
File parent = outputFile.getParentFile();
if (!parent.exists()) {
LOG
.info(String
.format(
"Got a file entry before the parent directory entry."
+ " Attempting to create the parent directory directory %s.",
parent.getAbsolutePath()));
if (!parent.mkdirs()) {
throw new IllegalStateException(String.format(
"Couldn't create directory %s.", parent
.getAbsolutePath()));
}
}
outputFile.createNewFile();
final OutputStream outputFileStream = new FileOutputStream(
outputFile);
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
unzippedFiles.add(outputFile);
}
debInputStream.close();
return unzippedFiles;
}