Java Byte Array Uncompress uncompress(byte[] gzData, String charset)

Here you can find the source of uncompress(byte[] gzData, String charset)

Description

uncompress

License

Apache License

Parameter

Parameter Description
gzData byte array containing gzipped buffer
charset character set decoding for text

Exception

Parameter Description
IOExceptionon error with decompression or text encoding

Return

buffer of uncompressed, decoded string

Declaration

public static String uncompress(byte[] gzData, String charset) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w . ja  va 2 s. co  m*/
 *
 * Copyright 2012-2013 The MITRE Corporation.
 *
 * Licensed 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.
 *
 * **************************************************************************
 * NOTICE This software was produced for the U. S. Government under Contract No.
 * W15P7T-12-C-F600, and is subject to the Rights in Noncommercial Computer
 * Software and Noncommercial Computer Software Documentation Clause
 * 252.227-7014 (JUN 1995)
 *
 * (c) 2012 The MITRE Corporation. All Rights Reserved.
 * **************************************************************************
 */

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

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * 
     * @param gzData   byte array containing gzipped buffer
     * @return buffer UTF-8 decoded string
     * 
     * @throws IOException  on error with decompression or text encoding
     */
    public static String uncompress(byte[] gzData) throws IOException {
        return uncompress(gzData, "UTF-8");
    }

    /**
     * 
     * @param gzData  byte array containing gzipped buffer
     * @param charset character set decoding for text
     * @return buffer of uncompressed, decoded string
     * @throws IOException  on error with decompression or text encoding
     */
    public static String uncompress(byte[] gzData, String charset) throws IOException {
        GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzData));
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        gzipInputStream.close();
        out.close();

        return new String(out.toByteArray(), charset);
    }
}

Related

  1. uncompress(byte[] b)
  2. uncompress(byte[] data)
  3. uncompress(byte[] input, int uncompr_len)
  4. uncompress(final byte[] buffer)
  5. uncompress(final byte[] compressedData)
  6. uncompress(final byte[] src)