ArchiveInputStream archiveInputStream = null;
ArchiveEntry entry;
try {
final CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputArchive));
final long inputFileSize = inputArchive.length();
if(inputArchive.getName().endsWith(".tbz2")) {
archiveInputStream = new TarArchiveInputStream(
new BZip2CompressorInputStream(inputStream));
} else {
archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(
new BufferedInputStream(inputStream));
}
final ProgressBar progressBar = new ProgressBar(inputFileSize);
while ((entry = archiveInputStream.getNextEntry()) != null) {
final File outputFile = new File(targetDirectory, entry.getName());
// Entry is a directory.
if (entry.isDirectory()) {
if (!outputFile.exists()) {
if(!outputFile.mkdirs()) {
throw new RetrieverException(
"Could not create output directory " + outputFile.getAbsolutePath());
}
}
}
// Entry is a file.
else {
final byte[] data = new byte[BUFFER_MAX];
final FileOutputStream fos = new FileOutputStream(outputFile);
BufferedOutputStream dest = null;
try {
dest = new BufferedOutputStream(fos, BUFFER_MAX);
int count;
while ((count = archiveInputStream.read(data, 0, BUFFER_MAX)) != -1) {
dest.write(data, 0, count);
progressBar.updateProgress(inputStream.getBytesRead());
}
} finally {
if(dest != null) {
dest.flush();
dest.close();
}
}
}
progressBar.updateProgress(inputStream.getBytesRead());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();