Example usage for java.util.zip GZIPInputStream GZIPInputStream

List of usage examples for java.util.zip GZIPInputStream GZIPInputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream GZIPInputStream.

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:Main.java

public static String decompress(byte[] bytes) {
    try {/*from   w ww .  j  a  v a2 s .c o m*/
        InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[262144]; // about 300kb
        int len;
        while ((len = in.read(buffer)) > 0)
            baos.write(buffer, 0, len);
        return new String(baos.toByteArray(), "UTF-8");
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static final byte[] unCompress(byte[] buf) throws IOException {
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
    ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

    int count;/*  w w w .  ja  v a2  s. c o  m*/
    byte[] tmp = new byte[2048];
    while ((count = gzi.read(tmp)) != -1) {
        bos.write(tmp, 0, count);
    }

    // store uncompressed back to buffer      
    gzi.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] gzipDecodeByteArray(byte[] data) {
    GZIPInputStream gzipInputStream = null;
    try {//from   w w  w . j a  v a 2 s.c o m
        gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(data));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (gzipInputStream.available() > 0) {
            int count = gzipInputStream.read(buffer, 0, 1024);
            if (count > 0) {
                //System.out.println("Read " + count + " bytes");
                outputStream.write(buffer, 0, count);
            }
        }

        outputStream.close();
        gzipInputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static InputStream buildInputStream(InputStream is, byte type) throws IOException {
    switch (type) {
    case GZIP:/*  w w  w  .j a  v  a  2s.c  om*/
        return new GZIPInputStream(is);
    default:
        return is;
    }
}

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {/*  w w w .j  a v  a2 s. c o m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] gunzip(byte[] data) throws IOException {
    InputStream in = new GZIPInputStream(new ByteArrayInputStream(data));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteStreams.copy(in, out);//ww w .j a  va2 s.  c om
    return out.toByteArray();
}

From source file:Main.java

public static String descomprimeComGZIP(byte[] bytes, String encoding) {
    String decompressedString = "";
    try {// w  w w.java2s  .  c  o m
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decompressedString;
}

From source file:Main.java

protected static ByteArrayOutputStream inflate(final FileInputStream pInputStream) throws IOException {
    final ByteArrayOutputStream uncompressed = new ByteArrayOutputStream(ONE_MB);
    final GZIPInputStream zin = new GZIPInputStream(pInputStream);

    int read;/*from   www.  j  a v  a2  s .  c o  m*/
    final byte[] buf = new byte[ONE_MB];
    while ((read = zin.read(buf)) != -1) {
        uncompressed.write(buf, 0, read);
    }

    return uncompressed;
}

From source file:Main.java

public static String decompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        return "";
    }//  www.  ja  v  a  2  s .co  m
    String outStr = "";
    try {
        final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
        final BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        String line;
        while ((line = bf.readLine()) != null) {
            outStr += line;
        }
        bf.close();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return outStr;
}

From source file:Main.java

/**
 * uncompress the xmlByteArray//from  w w  w. j a v  a 2  s  .c o  m
 */
public static byte[] uncompressByteArray(byte[] xmlByteArray) throws IOException {
    byte[] tmp = new byte[2048];
    int byteCount = 0;
    ByteArrayOutputStream uncompressedData = new ByteArrayOutputStream();
    GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(xmlByteArray));

    while ((byteCount = gzipIS.read(tmp)) != -1) {
        uncompressedData.write(tmp, 0, byteCount);
    }

    return uncompressedData.toByteArray();
}