Java Decompress Byte Array decompress(byte[] data)

Here you can find the source of decompress(byte[] data)

Description

Decompresses data.

License

Open Source License

Parameter

Parameter Description
data Data to be decompressed.

Exception

Parameter Description
IOException If there is an IO related error.
UnsupportedEncodingException If given encoding is not supported.

Return

String object in specified encoding.

Declaration

public static byte[] decompress(byte[] data) throws IOException, UnsupportedEncodingException 

Method Source Code


//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 ww  w .jav  a2 s  .c om*/
     * 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) {
            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

  1. decompress(byte[] compressedData)
  2. decompress(byte[] compressedData, int off, int len)
  3. decompress(byte[] data)
  4. decompress(byte[] data)
  5. decompress(byte[] data)
  6. decompress(byte[] data)
  7. decompress(byte[] data)
  8. decompress(byte[] data)
  9. decompress(byte[] data, int off, int len)