Java Deflate Byte Array deflateGzip(byte[] inputBytes)

Here you can find the source of deflateGzip(byte[] inputBytes)

Description

Deflate a gzip byte array.

License

Apache License

Parameter

Parameter Description
inputBytes a gzip compressed byte array

Exception

Parameter Description
IOException error in gzip input stream

Return

a deflated byte array

Declaration

public static byte[] deflateGzip(byte[] inputBytes) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.zip.GZIPInputStream;

public class Main {
    /**/*from   ww  w  .  j a v  a2 s  . c  o  m*/
     * The size of a chunk for a byte buffer.
     */
    private static final int BYTE_BUFFER_CHUNK_SIZE = 4096;

    /**
     * Deflate a gzip byte array.
     *
     * @param inputBytes a gzip compressed byte array
     * @return a deflated byte array
     * @throws IOException error in gzip input stream
     */
    public static byte[] deflateGzip(byte[] inputBytes) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try (GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(inputBytes))) {
            byte[] buffer = new byte[BYTE_BUFFER_CHUNK_SIZE];
            while (gzipInputStream.available() == 1) {
                int size = gzipInputStream.read(buffer);
                if (size == -1) {
                    break;
                }
                byteArrayOutputStream.write(buffer, 0, size);
            }
            return byteArrayOutputStream.toByteArray();
        }
    }
}

Related

  1. deflate(final byte[] pInput)
  2. deflate(String inString)
  3. deflate(String text, String encode)
  4. deflateBuffer(byte[] uncompressedBuffer)
  5. deflateByteArray(final byte[] array)
  6. deflateGzip(final byte[] bts)
  7. deflater(final byte[] inputByte)