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

static public byte[] gunzip(byte src[], byte default_value[]) {
    try {/* w  w w .  j  a  va2  s. c  o  m*/
        if (src == null)
            return default_value;

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(src));

        IOUtils.copy(in, out);

        in.close();
        out.close();

        return out.toByteArray();
    } catch (Exception e) {
        return default_value;
    }
}

From source file:Main.java

public static InputStream getInputEncoding(URLConnection connection) throws IOException {
    InputStream in;/*from   w w w  .  j a v a 2 s. c o  m*/
    String encoding = connection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(connection.getInputStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        in = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
    } else {
        in = connection.getInputStream();
    }
    return in;
}

From source file:Main.java

/**
 * TODO Finish JavaDoc/*from w w  w  . j a  v a2 s.  co m*/
 *
 * @param byteArray
 * @return
 */
public static String decompress(byte[] byteArray) {

    StringBuilder stringBuilder = new StringBuilder();
    String line;
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
            new GZIPInputStream(new ByteArrayInputStream(byteArray)), StandardCharsets.UTF_8))) {
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return stringBuilder.toString();
}

From source file:Main.java

private static void decompressGzipFile(String gzipFile) {
    try {/*from  w  w w .  j av a 2s.c o  m*/
        File newFile = new File(Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        FileInputStream fis = new FileInputStream(gzipFile);
        GZIPInputStream gis = new GZIPInputStream(fis);
        FileOutputStream fos = new FileOutputStream(newFile);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        //close resources
        fos.close();
        gis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static byte[] decompressGzip(byte[] compressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(bis);
    try {/*  w w  w.  ja  v a2s .  c o m*/
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        int read = 0;
        while ((read = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
        }
    } finally {
        gis.close();
    }

    return bos.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] bytes) throws IOException {

    GZIPInputStream gzip = null;/*from w w  w  .j a v  a 2 s.com*/
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();

        gzip = new GZIPInputStream(new ByteArrayInputStream(bytes));

        int len = 0;
        byte data[] = new byte[BUFFER_SIZE];

        while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) {
            baos.write(data, 0, len);
        }

        gzip.close();
        baos.flush();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:Main.java

/**
 * Decompresses a array of bytes back into a string.
 *
 * @param bytes The compressed list of bytes.
 *
 * @return The string uncompress./*from w w  w  . j ava2  s.c  o m*/
 *
 * @throws Exception If failed to uncompress.
 */
public static String decompress(byte[] bytes) throws Exception {
    if (bytes == null || bytes.length == 0) {
        return null;
    }

    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
    BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
    StringBuilder result = new StringBuilder();
    String line;

    while ((line = bf.readLine()) != null) {
        result.append(line);
    }

    return result.toString();
}

From source file:Main.java

/**
 * Descomprime uma string utilizando o GZIP
 * /*from   w w  w . j  a  v a  2  s. com*/
 * @param str
 * @param encoding
 * @return
 */
public static String gzipDecompress(byte[] bytes, String encoding) {
    String decompressedString = "";

    try {
        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

public static byte[] decompressInGzip(byte[] compressData, int offset, int length) throws Exception {

    ByteArrayInputStream bis = new ByteArrayInputStream(compressData, offset, length);
    GZIPInputStream gzipInStream = new GZIPInputStream(bis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    int count;//w w w .  j av a  2s. c  o  m
    byte[] buf = new byte[1024];
    while ((count = gzipInStream.read(buf)) > 0) {
        bos.write(buf, 0, count);
    }
    gzipInStream.close();

    byte[] originalData = bos.toByteArray();
    bos.close();

    return originalData;
}

From source file:Main.java

public static byte[] unzip(byte[] output) {
    if (output == null || output.length == 0) {
        return output;
    }/*from   w  ww.  jav  a  2  s .com*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(output);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[BUFFER_SIZE];
        int ret;
        while ((ret = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, ret);
        }
        gunzip.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}