Java – Uncompress BZIP2 archive


I can uncompress zip, gzip, and rar files, but I also need to uncompress bzip2 files as well as unarchive them (.tar). I haven't come across a good library to use.

I am using Java along with Maven so ideally, I'd like to include it as a dependency in the POM.

What libraries do you recommend?

Best Solution

The best option I can see is Apache Commons Compress with this Maven dependency.


From the examples:

FileInputStream in = new FileInputStream("archive.tar.bz2");
FileOutputStream out = new FileOutputStream("archive.tar");
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = {
  out.write(buffer, 0, n);
Related Question