Java Decompress Byte Array decompressGzipByteArray(byte[] compressedByteArray)

Here you can find the source of decompressGzipByteArray(byte[] compressedByteArray)

Description

decompress a gzip byte array, using a default buffer length of 1024

License

Apache License

Parameter

Parameter Description
compressedByteArray gzip-compressed byte array

Exception

Parameter Description
IOException thrown if there was a failure to construct the GzipInputStream

Return

decompressed byte array

Declaration

public static byte[] decompressGzipByteArray(byte[] compressedByteArray)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from www .  j a v a2s  .c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

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

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * decompress a gzip byte array, using a default buffer length of 1024
     * <p>
     * @param compressedByteArray gzip-compressed byte array
     * @return decompressed byte array
     * @throws IOException thrown if there was a failure to construct the GzipInputStream
     */
    public static byte[] decompressGzipByteArray(byte[] compressedByteArray)
            throws IOException {
        return decompressGzipByteArray(compressedByteArray, 1024);
    }

    /**
     * decompress a gzip byte array, using a default buffer length of 1024
     * <p>
     * @param compressedByteArray gzip-compressed byte array
     * @param bufferlength size of the buffer in bytes
     * @return decompressed byte array
     * @throws IOException thrown if there was a failure to construct the GzipInputStream
     */
    public static byte[] decompressGzipByteArray(
            byte[] compressedByteArray, int bufferlength)
            throws IOException {
        ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream();

        GZIPInputStream compressedStream = new GZIPInputStream(
                new ByteArrayInputStream(compressedByteArray));

        byte[] buffer = new byte[bufferlength];

        int index = -1;

        while ((index = compressedStream.read(buffer)) != -1) {
            uncompressedStream.write(buffer, 0, index);
        }

        return uncompressedStream.toByteArray();
    }
}

Related

  1. decompressByZLIB(byte[] compressedBytes)
  2. decompressData(byte[] compressedInput)
  3. decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
  4. decompressGzip(byte[] compressed)
  5. decompressGZIP(byte[] data)
  6. decompressObject(byte[] bytes)
  7. decompressString(byte[] compressed)
  8. decompressString(byte[] compressedData)
  9. decompressString(byte[] compressedString)