GZIP Decompresses data. - Java File Path IO

Java examples for File Path IO:GZIP

Description

GZIP Decompresses data.

Demo Code

/*****************************************************************************
 * Copyright (c) 2007 Jet Propulsion Laboratory,
 * California Institute of Technology.  All rights reserved
 *****************************************************************************/
//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;

public class Main {
    private static final int _BUFFER_SIZE = 1024;

    /**//from  w  w w. j  a  v  a2  s .  co m
     * Decompresses data.
     * 
     * @param data Data to be decompressed.
     * @return String object in specified encoding.
     * @throws IOException If there is an IO related error.
     * @throws UnsupportedEncodingException If given encoding is not supported.
     */
    public static byte[] decompress(byte[] data) throws IOException,
            UnsupportedEncodingException {
        byte[] buffer = new byte[_BUFFER_SIZE];
        ByteArrayInputStream bais = null;
        GZIPInputStream gis = null;
        ByteArrayOutputStream baos = null;

        int byteRead = 0;
        try {
            bais = new ByteArrayInputStream(data);
            gis = new GZIPInputStream(bais);
            baos = new ByteArrayOutputStream();

            while ((byteRead = gis.read(buffer, 0, buffer.length)) != -1) {
                baos.write(buffer, 0, byteRead);
            }
        } catch (Exception exception) {
            throw new IOException("Failed to decompress data.");
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }

                if (gis != null) {
                    gis.close();
                }

                if (bais != null) {
                    bais.close();
                }
            } catch (Exception exception) {
            }
        }
        return baos.toByteArray();
    }

    public static void decompress(InputStream inputStream,
            OutputStream outputStream) throws IOException {
        byte[] buffer = new byte[_BUFFER_SIZE];
        GZIPInputStream gis = null;

        try {
            gis = new GZIPInputStream(inputStream);

            int bytesRead = 0;
            while ((bytesRead = gis.read(buffer, 0, buffer.length)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (IOException exception) {
            exception.printStackTrace();
            throw exception;
        } finally {
            if (gis != null) {
                gis.close();
            }
        }
    }

    public static void decompress(File inputFile, File outputFile)
            throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(inputFile);
            fos = new FileOutputStream(outputFile);
            decompress(fis, fos);
        } catch (IOException exception) {
            throw exception;
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related Tutorials