Example usage for java.util.zip GZIPInputStream read

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

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String source = "s.gzip";
    GZIPInputStream in = new GZIPInputStream(new FileInputStream(source));
    String target = "outfile";
    OutputStream out = new FileOutputStream(target);
    byte[] buf = new byte[1024];
    int len;/*from w w w  . ja  v  a 2  s.  c  o  m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String inFilename = "infile.gzip";
    GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename));

    String outFilename = "outfile";
    OutputStream out = new FileOutputStream(outFilename);

    byte[] buf = new byte[1024];
    int len;//from w  w w .j ava2 s . com
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

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

From source file:Main.java

/**
 * uncompress the xmlByteArray//from   w w w .j av  a2 s . com
 */
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();
}

From source file:Main.java

public static final byte[] uncompress(final byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }//  www . j a  v a 2 s  . c o  m
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    byte[] buffer = new byte[bytes.length];
    int length;
    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        byte[] moreBytes = bos.toByteArray();
        bos.close();
        bais.close();
        gis.close();
        bos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static String uncompress(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        return null;
    }/*from ww  w.  j  a v a 2 s  . c  o  m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPInputStream gunzip = new GZIPInputStream(inputStream);
    byte[] buffer = new byte[256];
    int n;
    while ((n = gunzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
    }
    return out.toString();
}

From source file:Main.java

public static byte[] unzip(byte[] output) {
    if (output == null || output.length == 0) {
        return output;
    }//from   ww  w.j  a va  2 s .  c o  m
    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);
    }
}

From source file:Main.java

public static String decompress(String hexString) throws UnsupportedEncodingException {
    final byte[] byteArray = new byte[hexString.length() / 2];
    int k = 0;/*www  . j  a  v  a 2  s . c o  m*/
    for (int i = 0; i < byteArray.length; i++) {
        byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
        byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
        byteArray[i] = (byte) (high << 4 | low);
        k += 2;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(byteArray);

    try {
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return out.toString();
}

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 va 2  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:ByteUtils.java

public static byte[] unpackRaw(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);

    GZIPInputStream zis = new GZIPInputStream(bais);
    byte[] tmpBuffer = new byte[256];
    int n;/*w w  w .  ja v  a  2  s  .  co  m*/
    while ((n = zis.read(tmpBuffer)) >= 0)
        baos.write(tmpBuffer, 0, n);
    zis.close();

    return baos.toByteArray();
}

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 {/*  ww  w .  j  av  a  2 s  .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();
}