Java Gunzip Byte Array gunzip(byte[] bytes)

Here you can find the source of gunzip(byte[] bytes)

Description

Uncompresses a GZIP file.

License

Open Source License

Parameter

Parameter Description
bytes The compressed bytes.

Exception

Parameter Description
IOException if an I/O error occurs.

Return

The uncompressed bytes.

Declaration

public static byte[] gunzip(byte[] bytes) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.GZIPInputStream;

public class Main {
    /**//w  ww  .  j av a 2  s .c  om
     * Uncompresses a GZIP file.
     * @param bytes The compressed bytes.
     * @return The uncompressed bytes.
     * @throws IOException if an I/O error occurs.
     */
    public static byte[] gunzip(byte[] bytes) throws IOException {
        /* create the streams */
        try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(
                bytes))) {
            try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
                /* copy data between the streams */
                byte[] buf = new byte[4096];
                int len;
                while ((len = is.read(buf, 0, buf.length)) != -1) {
                    os.write(buf, 0, len);
                }

                /* return the uncompressed bytes */
                return os.toByteArray();
            }
        }
    }
}

Related

  1. gunzip(byte src[], byte default_value[])
  2. gunzip(byte[] bytes)
  3. gunzip(byte[] bytes)
  4. gunzip(byte[] contentBytes)
  5. gunzip(byte[] data)