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:me.j360.trace.example.consumer2.ZipkinServerConfiguration.java

static byte[] gunzip(byte[] input) throws IOException {
    GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(input));
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length)) {
        byte[] buf = GZIP_BUFFER.get();
        int len;/*from ww  w .j ava 2s  . c om*/
        while ((len = in.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        return outputStream.toByteArray();
    }
}

From source file:org.eclipse.smila.connectivity.framework.crawler.web.util.GZIPUtils.java

/**
 * Returns an gunzipped copy of the input array.
 * //from   w w  w .  j  av a  2s  .  c  om
 * @param in
 *          input byte array
 * @return gunzipped copy of the input array
 * @throws IOException
 *           if the input cannot be properly decompressed
 */
public static byte[] unzip(byte[] in) throws IOException {
    // decompress using GZIPInputStream
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

    final GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in));

    final byte[] buf = new byte[BUF_SIZE];
    while (true) {
        final int size = inStream.read(buf);
        if (size <= 0) {
            break;
        }
        outStream.write(buf, 0, size);
    }
    outStream.close();

    return outStream.toByteArray();
}

From source file:edu.harvard.i2b2.fhir.Utils.java

public static String unCompressString(final byte[] data, final String encoding) throws IOException {
    if (data == null || data.length == 0) {
        return null;
    } else {/*ww w . j  a  v  a  2s.co  m*/
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        GZIPInputStream is = new GZIPInputStream(bais);
        byte[] tmp = new byte[256];
        while (true) {
            int r = is.read(tmp);
            if (r < 0) {
                break;
            }
            buffer.write(tmp, 0, r);
        }
        is.close();

        byte[] content = buffer.toByteArray();
        return new String(content, 0, content.length, encoding);
    }
}

From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java

public static byte[] decompress(byte[] contentBytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPInputStream bis = new GZIPInputStream(new Base64InputStream(new ByteArrayInputStream(contentBytes)));
    byte[] buffer = new byte[1024 * 4];
    int n = 0;//from   w  w  w  .  j av  a  2  s  . c  o  m
    while (-1 != (n = bis.read(buffer))) {
        out.write(buffer, 0, n);
    }
    bis.close();
    return out.toByteArray();
}

From source file:GZIPUtils.java

/**
 * Returns an gunzipped copy of the input array, truncated to
 * <code>sizeLimit</code> bytes, if necessary.  If the gzipped input
 * has been truncated or corrupted, a best-effort attempt is made to
 * unzip as much as possible.  If no data can be extracted
 * <code>null</code> is returned.
 *///from  ww w.ja  v a  2s  . c o  m
public static final byte[] unzipBestEffort(byte[] in, int sizeLimit) {
    try {
        // decompress using GZIPInputStream 
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

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

        byte[] buf = new byte[BUF_SIZE];
        int written = 0;
        while (true) {
            try {
                int size = inStream.read(buf);
                if (size <= 0)
                    break;
                if ((written + size) > sizeLimit) {
                    outStream.write(buf, 0, sizeLimit - written);
                    break;
                }
                outStream.write(buf, 0, size);
                written += size;
            } catch (Exception e) {
                break;
            }
        }
        try {
            outStream.close();
        } catch (IOException e) {
        }

        return outStream.toByteArray();

    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;
    try {/*from  www. j  av a2s.  com*/
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

public static void uncompressFile(File archive, File target) throws IOException {
    byte[] buffer = new byte[1024];
    if (target.exists())
        target.delete();//w  w w.ja v  a  2s.  c  o  m
    target.createNewFile();

    try {
        GZIPInputStream stream = new GZIPInputStream(new FileInputStream(archive));
        FileOutputStream out = new FileOutputStream(target);

        int len;
        while ((len = stream.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        stream.close();
        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    Bukkit.getLogger().info("Extracted file " + target.getName() + ".");
}

From source file:com.ery.ertc.estorm.util.GZIPUtils.java

/**
 * Returns an gunzipped copy of the input array, truncated to <code>sizeLimit</code> bytes, if necessary. If the gzipped input has been
 * truncated or corrupted, a best-effort attempt is made to unzip as much as possible. If no data can be extracted <code>null</code> is
 * returned.//from  w ww .j  a v  a2 s .c  om
 */
public static final byte[] unzipBestEffort(byte[] in, int sizeLimit) {
    try {
        // decompress using GZIPInputStream
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

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

        byte[] buf = new byte[BUF_SIZE];
        int written = 0;
        while (true) {
            try {
                int size = inStream.read(buf);
                if (size <= 0)
                    break;
                if ((written + size) > sizeLimit) {
                    outStream.write(buf, 0, sizeLimit - written);
                    break;
                }
                outStream.write(buf, 0, size);
                written += size;
            } catch (Exception e) {
                break;
            }
        }
        try {
            outStream.close();
        } catch (IOException e) {
        }

        return outStream.toByteArray();

    } catch (IOException e) {
        return null;
    }
}

From source file:pl.otros.logview.api.io.Utils.java

private static byte[] ungzip(byte[] buff) throws IOException {
    GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(buff));
    byte[] ungzippedRead = new byte[buff.length];
    int read = gzipInputStream.read(ungzippedRead);
    byte[] ungzipped = new byte[read];
    System.arraycopy(ungzippedRead, 0, ungzipped, 0, read);
    return ungzipped;
}

From source file:org.commoncrawl.util.shared.GZIPUtils.java

/**
 * Returns an gunzipped copy of the input array, truncated to
 * <code>sizeLimit</code> bytes, if necessary. If the gzipped input has been
 * truncated or corrupted, a best-effort attempt is made to unzip as much as
 * possible. If no data can be extracted <code>null</code> is returned.
 *//*www. java 2 s  .co  m*/
public static final UnzipResult unzipBestEffort(byte[] in, int offset, int sizeIn, int sizeLimit) {

    try {
        // decompress using GZIPInputStream
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

        boolean truncated = false;

        GZIPInputStream inStream = new GZIPInputStream(new ByteArrayInputStream(in, offset, sizeIn));

        byte[] buf = new byte[BUF_SIZE];
        int written = 0;
        while (true) {
            try {
                int size = inStream.read(buf);
                if (size <= 0)
                    break;
                if ((written + size) > sizeLimit) {
                    outStream.write(buf, 0, sizeLimit - written);
                    truncated = true;
                    break;
                }
                outStream.write(buf, 0, size);
                written += size;
            } catch (Exception e) {
                break;
            }
        }
        try {
            outStream.close();
        } catch (IOException e) {
        }

        return new UnzipResult(outStream.toByteArray(), truncated);

    } catch (IOException e) {
        return null;
    } catch (OutOfMemoryError e) {
        LOG.fatal(CCStringUtils.stringifyException(e));
        return null;
    }
}