Reputation: 89
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.
Upvotes: 0
Views: 620
Reputation: 13960
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"
Upvotes: 1
Reputation: 43259
The normal pattern is a file with name x gets saved as x.bz2, so the output file name is the input file name with the last four characters removed. The only known exception is x.tar -> x.tbz (but some people use x.tar.bz2).
This means your example doesn't follow the normal pattern; otherwise it would be test.txt.bz2.
Upvotes: 1