Search code examples
javabzip2compression

BZip2 - What to be passed to FileOutputstream for different types of files


I am trying to extract a bz2 file as mentioned below, this is a test class that I wrote and I know that it is .txt file when uncompressed, but when I actually read it from the server, the uncompressed bz2 file can be anything like html, tar,tgz or text files, how would I be able to make this code generic such that it will work for any kind of file.

I want to uncompress different files, if it is test.txt.bz2, then uncompress to test.txt and 6223.webvis.html_20130803195241.bz2 to 6223.webvis.html_20130803195241. How can I make my code generic such that it will work for these two different scenarios.

try{
FileInputStream fin = new FileInputStream("C:\\temp\\test.txt.bz2");
BufferedInputStream in = new BufferedInputStream(fin);
FileOutputStream out = new FileOutputStream("C:\\temp\\test.txt");
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
int buffersize = 1024;
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = bzIn.read(buffer))) {
out.write(buffer, 0, n);
}
out.close();
bzIn.close();
}
catch (Exception e) {
throw new Error(e.getMessage());
}
}

Thanks, Akshitha.


Solution

  • A BZ2 archive does not know anything about the original name. The usual way to do it is to compress file.ext as file.ext.bz2, so you get the output file name from the archive name.

    String inFile = "test.bz2";
    String outFile = inFile.substring(0, inFile.length() - 4);
    // outFile == "test"