Java Decompress Byte Array decompress(final byte[] compressed)

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

Description

decompress

License

Open Source License

Declaration

public static String decompress(final byte[] compressed) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.zip.GZIPInputStream;

public class Main {
    public static String decompress(final byte[] compressed) throws IOException {
        String outStr = "";
        if ((compressed == null) || (compressed.length == 0)) {
            return "";
        }//from w  ww  .j  a v  a 2  s . c  om
        if (isCompressed(compressed)) {
            GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                outStr += line + "\n";
            }
        } else {
            outStr = new String(compressed);
        }
        return outStr;
    }

    public static boolean isCompressed(final byte[] compressed) {
        return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
                && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
    }
}

Related

  1. decompress(byte[] source)
  2. decompress(byte[] source)
  3. decompress(byte[] src, Inflater decompresser, int compressCycleSize)
  4. decompress(byte[] str)
  5. decompress(byte[] zipByte)
  6. decompress(final DataInputStream input, final byte[] result, int offset, int length)
  7. decompressAndB64DecodeUTF8Bytes(byte[] b64EncodedCompressedBytes)
  8. decompressByte(byte[] decompress)
  9. decompressByteArray(byte[] compressedData)