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: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.ja va  2 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:com.sonatype.nexus.perftest.maven.HttpdLogParser.java

@JsonCreator
public HttpdLogParser(@JsonProperty("logfile") File logfile) throws IOException {
    ArrayList<String> paths = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream(logfile))))) {
        String str;//  w ww . j  a v a 2  s  .  c o m
        while ((str = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(str, "[]\" ");
            st.nextToken(); // ip
            st.nextToken(); // not sure
            st.nextToken(); // username
            st.nextToken(); // [date:time
            st.nextToken(); // timezoneoffset]
            String method = st.nextToken(); // "METHOD
            if ("GET".equals(method)) {
                String path = st.nextToken(); // path
                if (path.startsWith(PREFIX)) {
                    paths.add(path.substring(PREFIX.length()));
                }
            }
        }
    }
    this.paths = Collections.unmodifiableList(paths);
}

From source file:com.useekm.types.AbstractGeo.java

public static byte[] gunzip(byte[] bytes) {
    try {//from   w  w  w. j a  v a 2  s.c o m
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0)
            bos.write(buf, 0, len);
        byte[] result = bos.toByteArray();
        bufis.close();
        bos.close();
        return result;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gunzip", e);
    }
}

From source file:com.marklogic.contentpump.GzipDelimitedJSONReader.java

@Override
protected void initFileStream(InputSplit inSplit) throws IOException, InterruptedException {
    setFile(((FileSplit) inSplit).getPath());
    configFileNameAsCollection(conf, file);
    fileIn = fs.open(file);/*w  w w  .j av  a 2s  .  co  m*/
    gzipIn = new GZIPInputStream(fileIn);
    instream = new InputStreamReader(gzipIn, encoding);
    reader = new LineNumberReader(instream);
}

From source file:bioLockJ.AppController.java

/**
 * Get a BufferedReader for standard text file or gzipped file.
 * @param file//from   ww  w.j  a v a  2s .co m
 * @return
 * @throws Exception
 */
public static BufferedReader getFileReader(final File file) throws Exception {
    return file.getName().toLowerCase().endsWith(".gz")
            ? new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))))
            : new BufferedReader(new FileReader(file));
}

From source file:com.ryan.ryanreader.cache.GzipDecompressingEntity.java

public InputStream getContent() throws IOException, IllegalStateException {

    // the wrapped entity's getContent() decides about repeatability
    final InputStream wrappedin = wrappedEntity.getContent();

    return new GZIPInputStream(wrappedin);
}

From source file:org.droidparts.http.worker.HTTPInputStream.java

private static InputStream getUnpackedInputStream(String contentEncoding, InputStream is) throws IOException {
    L.d("Content-Encoding: " + contentEncoding);
    if (isNotEmpty(contentEncoding)) {
        contentEncoding = contentEncoding.toLowerCase();
        if (contentEncoding.contains("gzip")) {
            return new GZIPInputStream(is);
        } else if (contentEncoding.contains("deflate")) {
            return new InflaterInputStream(is);
        }//from  w  ww . ja v  a  2  s  .c o m
    }
    return is;
}

From source file:pj.rozkladWKD.HttpClient.java

public static JSONObject SendHttpPost(List<NameValuePair> nameValuePairs)
        throws JSONException, ClientProtocolException, IOException {

    // Create a new HttpClient and Post Header  
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mmaj.nazwa.pl/rozkladwkd/listener2.php");
    nameValuePairs.add(new BasicNameValuePair("app_ver", RozkladWKD.APP_VERSION));

    // Add your data  
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF_8"));

    // Execute HTTP Post Request  
    HttpResponse response = httpclient.execute(httppost);

    HttpEntity entity = response.getEntity();

    if (entity != null) {
        // Read the content stream

        InputStream instream = entity.getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            instream = new GZIPInputStream(instream);
        }// w ww  .j  a  va2  s . c o  m

        // convert content stream to a String
        String resultString = convertStreamToString(instream);

        instream.close();
        //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

        // Transform the String into a JSONObject
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "result: " + resultString);
        }
        JSONObject jsonObjRecv = new JSONObject(resultString);
        // Raw DEBUG output of our received JSON object:
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>");
        }

        return jsonObjRecv;
    }

    return null;
}

From source file:at.bitfire.davdroid.webdav.GzipDecompressingEntity.java

@Override
public InputStream getContent() throws IOException, IllegalStateException {

    // the wrapped entity's getContent() decides about repeatability
    InputStream wrappedin = wrappedEntity.getContent();

    return new GZIPInputStream(wrappedin);
}

From source file:de.dentrassi.pm.npm.aspect.NpmExtractor.java

private void perform(final Path file, final Map<String, String> metadata) throws IOException {
    try (final GZIPInputStream gis = new GZIPInputStream(new FileInputStream(file.toFile()));
            final TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
        TarArchiveEntry entry;//from w w  w.j  av a2s . c  o m
        while ((entry = tis.getNextTarEntry()) != null) {
            if (entry.getName().equals("package/package.json")) {
                final byte[] data = new byte[(int) entry.getSize()];
                ByteStreams.read(tis, data, 0, data.length);

                final String str = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(data)).toString();

                try {
                    // test parse
                    new JsonParser().parse(str);
                    // store
                    metadata.put("package.json", str);
                } catch (final JsonParseException e) {
                    // ignore
                }

                break; // stop parsing the archive
            }
        }

    }
}