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: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  w  w . j  a  va 2  s.  co  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:com.breadwallet.tools.util.BRCompressor.java

public static byte[] gZipExtract(byte[] compressed) {
    if (compressed == null || compressed.length == 0)
        return null;
    try {/*  w  w w  .  j av  a 2s.  co m*/
        InputStream isr = new GZIPInputStream(new ByteArrayInputStream(compressed));
        return IOUtils.toByteArray(isr);
    } catch (IOException e) {
        FirebaseCrash.report(e);
        e.printStackTrace();
    }
    return null;
}

From source file:hr.fer.zemris.vhdllab.remoting.GzipHttpInvokerServiceExporter.java

@Override
protected InputStream decorateInputStream(HttpServletRequest request, InputStream is) throws IOException {
    return new GZIPInputStream(is);
}

From source file:net.fenyo.mail4hotspot.service.Browser.java

public static String getHtml(final String target_url, final Cookie[] cookies) throws IOException {
    // log.debug("RETRIEVING_URL=" + target_url);
    final URL url = new URL(target_url);

    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.69.60.6", 3128)); 
    // final HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

    //      HttpURLConnection.setFollowRedirects(true);
    // conn.setRequestProperty("User-agent", "my agent name");

    conn.setRequestProperty("Accept-Language", "en-US");

    //      conn.setRequestProperty(key, value);

    // allow both GZip and Deflate (ZLib) encodings
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    final String encoding = conn.getContentEncoding();
    InputStream is = null;//from   www  . j  a va2s. c o  m
    // create the appropriate stream wrapper based on the encoding type
    if (encoding != null && encoding.equalsIgnoreCase("gzip"))
        is = new GZIPInputStream(conn.getInputStream());
    else if (encoding != null && encoding.equalsIgnoreCase("deflate"))
        is = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
    else
        is = conn.getInputStream();

    final InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is));

    final CharBuffer cb = CharBuffer.allocate(1024 * 1024);
    int ret;
    do {
        ret = reader.read(cb);
    } while (ret > 0);
    cb.flip();
    return cb.toString();
}

From source file:de.blizzy.backup.Compression.java

public InputStream getInputStream(InputStream in) throws IOException {
    if (this == GZIP) {
        return new GZIPInputStream(in);
    }//  w w w . j  a  v a  2  s  .  c om
    if (this == BZIP2) {
        return new BZip2CompressorInputStream(in);
    }
    throw new RuntimeException();
}

From source file:com.insightml.utils.io.IoUtils.java

public static String readFile(final File file) {
    try {// w  ww . j av a2 s  . co m
        if (file.getName().endsWith(".gz")) {
            return readFile(new GZIPInputStream(new FileInputStream(file)), Charsets.UTF_8);
        }
        return Files.toString(file, Charsets.UTF_8);
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:ch.cyberduck.core.http.GzipDecompressingEntity.java

/**
 * Decompress compressed entity using GZIP
 *
 * @return//  w ww  .  ja  v  a2s .co m
 * @throws IOException
 * @throws IllegalStateException
 */
@Override
public InputStream getContent() throws IOException, IllegalStateException {
    InputStream wrapped = wrappedEntity.getContent();
    // the wrapped entity's getContent() decides about repeatability
    return new GZIPInputStream(wrapped);
}

From source file:com.wenzani.maven.mongodb.TarUtils.java

public String untargz(File archive, File outputDir) {
    String absolutePath = archive.getAbsolutePath();
    String root = null;//www  . ja v a  2s  . c om
    boolean first = true;

    while (absolutePath.contains("tar") || absolutePath.contains("gz") || absolutePath.contains("tgz")) {
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("."));
    }

    absolutePath = absolutePath + ".tar";

    try {
        GZIPInputStream input = new GZIPInputStream(new FileInputStream(archive));
        FileOutputStream fos = new FileOutputStream(new File(absolutePath));

        IOUtils.copy(input, fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fos);

        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
                new FileInputStream(absolutePath));

        for (TarArchiveEntry entry = tarArchiveInputStream.getNextTarEntry(); entry != null;) {
            unpackEntries(tarArchiveInputStream, entry, outputDir);

            if (first && entry.isDirectory()) {
                root = outputDir + File.separator + entry.getName();
            }

            entry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return root;
}

From source file:com.proofpoint.http.server.GZipRequestWrapper.java

@Override
public ServletInputStream getInputStream() throws IOException {
    return new ServletInputStreamFromInputStream(new GZIPInputStream(request.getInputStream()));
}

From source file:com.ctriposs.r2.filter.compression.GzipCompressor.java

@Override
public byte[] inflate(InputStream data) throws CompressionException {
    ByteArrayOutputStream out;/*  ww w .j a  v  a 2 s .com*/
    GZIPInputStream gzip = null;

    try {
        out = new ByteArrayOutputStream();
        gzip = new GZIPInputStream(data);

        IOUtils.copy(gzip, out);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
    } finally {
        if (gzip != null) {
            IOUtils.closeQuietly(gzip);
        }
    }

    return out.toByteArray();
}