Search code examples
javabzip2gzip

How to read a .gz or .bzip2 file in java


I have .gz and .bzip2 files and I need this to be extracted and displayed. I looked at couple of places and it mentions to use zip4j utility. Can I use this to extract and display the files? Please let me know.

I referred the post Uncompress BZIP2 archive and tried to implement as below, but I am not sure what to pass to FileOutputStream and it is giving me an error.Also,is it possible to create a file object after its uncompressed.Please assist. Thank you for all the help again.

 if (getExtension(filename).equals("bz2")) {
                        try {
                        FileInputStream fin = new FileInputStream("C:\\temp\\test.bz2");
                        BufferedInputStream in = new BufferedInputStream(fin);
                        FileOutputStream out = new FileOutputStream("archive.tar");
                        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 in advance.

~Akshitha


Solution

  • .gz file extension is an indication for using GZIP compression. You can use GZIPInputStream for opening GZIP in java 7 SDK: http://docs.oracle.com/javase/7/docs/api/java/util/zip/GZIPInputStream.html

    BZIP2 compression / decompression is not supported, afaik, by Java SDK libraries and you will have to use a 3rd party library such as Apache Commons Compress

    The lib you mentioned seem (from a very brief view) to support only ZIP format, not the GZIP of BZIP2 variants - which are different.

    Please note that unlike ZIP compression, GZIP compress all files into one pack - so you cannot extract a single file without decompressing the entire package (or maybe until you reached all the bytes you wanted).