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.carrotsearch.util.httpclient.HttpUtils.java

/**
 * Opens a HTTP/1.1 connection to the given URL using the GET method, decompresses
 * compressed response streams, if supported by the server.
 * //from   w w  w.  j  a va  2s. co m
 * @param url The URL to open. The URL must be properly escaped, this method will
 *            <b>not</b> perform any escaping.
 * @param params Query string parameters to be attached to the url.
 * @param headers Any extra HTTP headers to add to the request.
 * @return The {@link HttpUtils.Response} object. Note that entire payload is read and
 *         buffered so that the HTTP connection can be closed when leaving this
 *         method.
 */
public static Response doGET(String url, Collection<NameValuePair> params, Collection<Header> headers)
        throws HttpException, IOException {
    final HttpClient client = HttpClientFactory.getTimeoutingClient();
    client.getParams().setVersion(HttpVersion.HTTP_1_1);

    final GetMethod request = new GetMethod();
    final Response response = new Response();
    try {
        request.setURI(new URI(url, true));

        if (params != null) {
            request.setQueryString(params.toArray(new NameValuePair[params.size()]));
        }

        request.setRequestHeader(HttpHeaders.URL_ENCODED);
        request.setRequestHeader(HttpHeaders.GZIP_ENCODING);
        if (headers != null) {
            for (Header header : headers)
                request.setRequestHeader(header);
        }

        Logger.getLogger(HttpUtils.class).debug("GET: " + request.getURI());

        final int statusCode = client.executeMethod(request);
        response.status = statusCode;

        InputStream stream = request.getResponseBodyAsStream();
        final Header encoded = request.getResponseHeader("Content-Encoding");
        if (encoded != null && "gzip".equalsIgnoreCase(encoded.getValue())) {
            stream = new GZIPInputStream(stream);
            response.compression = COMPRESSION_GZIP;
        } else {
            response.compression = COMPRESSION_NONE;
        }

        final Header[] respHeaders = request.getResponseHeaders();
        response.headers = new String[respHeaders.length][];
        for (int i = 0; i < respHeaders.length; i++) {
            response.headers[i] = new String[] { respHeaders[i].getName(), respHeaders[i].getValue() };
        }

        response.payload = StreamUtils.readFullyAndClose(stream);
        return response;
    } finally {
        request.releaseConnection();
    }
}

From source file:eu.itesla_project.modules.histo.tools.HistoDbPrintAttributesTool.java

private static Reader createReader(InputStream is, boolean zipped) throws IOException {
    return zipped ? new InputStreamReader(new GZIPInputStream(is), StandardCharsets.UTF_8)
            : new InputStreamReader(is, StandardCharsets.UTF_8);
}

From source file:com.nanosheep.bikeroute.parser.XMLParser.java

protected InputStream getInputStream() {
    try {//from   w w w  . jav  a 2s .com
        HttpUriRequest request = new HttpGet(feedUrl.toString());
        request.addHeader("Accept-Encoding", "gzip");
        request.addHeader("User-Agent", BikeRouteConsts.AGENT);
        final HttpResponse response = new DefaultHttpClient().execute(request);
        Header ce = response.getFirstHeader("Content-Encoding");
        String contentEncoding = null;
        if (ce != null) {
            contentEncoding = ce.getValue();
        }
        InputStream instream = response.getEntity().getContent();
        if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
            instream = new GZIPInputStream(instream);
        }
        return instream;
    } catch (IOException e) {
        Log.e("XML parser", e.getMessage() + feedUrl);
        return null;
    }
}

From source file:indexing.WTDocIndexer.java

void indexTarArchive() throws Exception {
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(new File(tarArchivePath)));
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream);
    TarArchiveEntry tarEntry = tarArchiveInputStream.getNextTarEntry();
    while (tarEntry != null) {
        if (!tarEntry.isDirectory()) {
            System.out.println("Extracting " + tarEntry.getName());
            final OutputStream outputFileStream = new FileOutputStream(tarEntry.getName());
        } else {//  www.ja  v a2s . c o  m
            tarEntry = tarArchiveInputStream.getNextTarEntry();
        }
    }
}

From source file:com.izforge.izpack.core.io.FileSpanningInputStream.java

/**
 * Constructs a <tt>FileSpanningInputStream</tt>.
 *
 * @param volume  the first volume to read
 * @param volumes the no. of volumes/*from ww w  .jav a 2 s  .c o m*/
 * @throws CorruptVolumeException if the volume magic no. cannot be read
 * @throws IOException            for any other I/O exception
 */
public FileSpanningInputStream(File volume, int volumes) throws IOException {
    spanningInputStream = new SpanningInputStream(volume, volumes);
    zippedInputStream = new GZIPInputStream(spanningInputStream);
}

From source file:GZIPUtils.java

/**
 * Returns an gunzipped copy of the input array.  
 * @throws IOException if the input cannot be properly decompressed
 *//*from  w  ww. ja v  a  2 s . c o  m*/
public static final byte[] unzip(byte[] in) throws IOException {
    // 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];
    while (true) {
        int size = inStream.read(buf);
        if (size <= 0)
            break;
        outStream.write(buf, 0, size);
    }
    outStream.close();

    return outStream.toByteArray();
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.ArchiveCompressorTarGzImplFastTest.java

@Test
public void testCompress() throws IOException {

    TarInputStream tin = null;/* w w w .  j  a  va2 s .  c o  m*/

    try {
        String testDir = SAMPLE_DIR + "qclive/compression/tarGz/";
        File file1 = new File(testDir + "file1.txt");
        File file2 = new File(testDir + "file2.txt");
        File file3 = new File(testDir + "file3.txt");
        List<File> files = new ArrayList<File>();
        files.add(file1);
        files.add(file2);
        files.add(file3);
        // pass in a list of files and verify that a tar.gz file is made
        ArchiveCompressor compressor = new ArchiveCompressorTarGzImpl();
        String archiveName = "test";
        File createdFile = compressor.createArchive(files, archiveName, new File(testDir), true);
        File testFile = new File(testDir + File.separator + archiveName + compressor.getExtension());
        assertEquals(testFile.getCanonicalPath(), createdFile.getCanonicalPath());
        assertTrue(testFile.exists());
        // look at tar entries and make sure expected files are there, with just
        // archive name as the path
        GZIPInputStream gzipStream = new GZIPInputStream(new FileInputStream(testFile));
        //noinspection IOResourceOpenedButNotSafelyClosed
        tin = new TarInputStream(gzipStream);
        List<String> tarEntryNames = new ArrayList<String>();
        TarEntry tarEntry;
        while ((tarEntry = tin.getNextEntry()) != null) {
            tarEntryNames.add(tarEntry.getName());
        }
        tin.close();
        gzipStream.close();
        assertTrue(tarEntryNames.contains(archiveName + File.separator + "file1.txt"));
        assertTrue(tarEntryNames.contains(archiveName + File.separator + "file2.txt"));
        assertTrue(tarEntryNames.contains(archiveName + File.separator + "file2.txt"));
        testFile.deleteOnExit();
    } finally {
        IOUtils.closeQuietly(tin);
    }
}

From source file:com.zaa.WeatherLoad.java

public void Load() {
    HttpURLConnection lv_connection = null;
    try {/*from   w  w w . j a va 2 s  . c  o  m*/
        City.InitCity();
        String lv_name, lv_dt, lv_encoding, lv_response;
        Set lt_keys = Cache.gv_city.keySet();
        Iterator lv_iterator = lt_keys.iterator();
        JSONArray lv_json_array, lv_json_weather;
        JSONObject lv_json, lv_json_response, lv_json_weather_row, lv_json_temp;
        City lv_city;
        CityTemp lv_city_temp;
        int lv_resp_code;
        InputStream lv_in_stream;
        Hashtable lv_city_temp_col;
        while (lv_iterator.hasNext()) {
            lv_name = lv_iterator.next().toString();
            lv_city_temp_col = (Hashtable) Cache.gv_city_temp.get(lv_name);
            lv_connection = (HttpURLConnection) new URL("http://api.openweathermap.org/data/2.5/weather?q="
                    + URLEncoder.encode(lv_name, "utf-8") + "&units=metric").openConnection();
            lv_connection.setConnectTimeout(30000);
            lv_connection.setReadTimeout(30000);
            lv_connection.setDoOutput(true);
            lv_connection.setDoInput(true);
            lv_connection.setRequestMethod("GET");
            lv_connection.setRequestProperty("Accept-Encoding", "gzip");
            lv_resp_code = lv_connection.getResponseCode();
            lv_in_stream = new BufferedInputStream(lv_connection.getInputStream(), 8192);
            lv_encoding = lv_connection.getHeaderField("Content-Encoding");
            if (lv_encoding != null && lv_encoding.equalsIgnoreCase("gzip")) {
                lv_in_stream = new GZIPInputStream(lv_in_stream);
            }
            lv_response = convertStreamToString(lv_in_stream);
            lv_json_response = new JSONObject(lv_response);
            if (lv_json_response.getString("cod").equals("200")) {
                lv_city = (City) Cache.gv_city.get(lv_name);
                lv_json = lv_json_response.getJSONObject("main");
                lv_city.SetTemp(lv_json.getString("temp"));
                lv_city.SetPressure(lv_json.getString("pressure"));
                lv_city.SetHumidity(lv_json.getString("humidity"));
                lv_json = lv_json_response.getJSONObject("wind");
                lv_city.SetWindSpeed(lv_json.getString("speed"));
                lv_json_weather = lv_json_response.getJSONArray("weather");
                if (lv_json_weather != null) {
                    lv_json_weather_row = (JSONObject) lv_json_weather.get(0);
                    lv_city.SetIcon(lv_json_weather_row.getString("icon").substring(0, 2));
                }
                lv_city.Save();
            }
            lv_connection.disconnect();
            lv_connection = (HttpURLConnection) new URL(
                    "http://api.openweathermap.org/data/2.5/forecast/daily?q="
                            + URLEncoder.encode(lv_name, "utf-8") + "&units=metric&cnt=7").openConnection();
            lv_connection.setConnectTimeout(30000);
            lv_connection.setReadTimeout(30000);
            lv_connection.setDoOutput(true);
            lv_connection.setDoInput(true);
            lv_connection.setRequestMethod("GET");
            lv_connection.setRequestProperty("Accept-Encoding", "gzip");
            lv_resp_code = lv_connection.getResponseCode();
            lv_in_stream = new BufferedInputStream(lv_connection.getInputStream(), 8192);
            lv_encoding = lv_connection.getHeaderField("Content-Encoding");
            if (lv_encoding != null && lv_encoding.equalsIgnoreCase("gzip")) {
                lv_in_stream = new GZIPInputStream(lv_in_stream);
            }
            lv_response = convertStreamToString(lv_in_stream);
            lv_json_response = new JSONObject(lv_response);
            if (lv_json_response.getString("cod").equals("200")) {
                CityTemp.DeleteCityTemp(lv_name);
                lv_json_array = lv_json_response.getJSONArray("list");
                if (lv_json_array != null) {
                    for (int i = 0; i < lv_json_array.length(); i++) {
                        lv_json = (JSONObject) lv_json_array.get(i);
                        lv_dt = lv_json.getString("dt");
                        lv_city_temp = (CityTemp) lv_city_temp_col.get(lv_dt);
                        if (lv_city_temp == null) {
                            lv_city_temp = new CityTemp(lv_name, lv_dt);
                            lv_city_temp_col.put(lv_dt, lv_city_temp);
                        }
                        lv_json_weather = lv_json.getJSONArray("weather");
                        if (lv_json_weather != null) {
                            lv_json_weather_row = (JSONObject) lv_json_weather.get(0);
                            lv_city_temp.SetIcon(lv_json_weather_row.getString("icon").substring(0, 2));
                        }
                        lv_json_temp = lv_json.getJSONObject("temp");
                        lv_city_temp.SetTempDay(lv_json_temp.getString("day"));
                        lv_city_temp.SetTempNight(lv_json_temp.getString("night"));
                        lv_city_temp.Save();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        gv_handler.sendEmptyMessage(Weather.NO_NET);
    } finally {
        if (lv_connection != null) {
            lv_connection.disconnect();
        }
    }
    gv_handler.sendEmptyMessage(Weather.LOAD_OK);
}

From source file:edu.sdsc.solr.lemmatization.LemmatizationWriter.java

static void downloadDictionary(URL definitionUrl, File target) throws IOException {
    try (InputStream is = new GZIPInputStream(definitionUrl.openConnection().getInputStream())) {
        FileUtils.copyInputStreamToFile(is, target);
    }/*from w ww . j  ava 2 s . co m*/
}

From source file:fr.eoit.util.DownloadUtil.java

@Override
public InputStream urlToInputStream(Context context, String url) throws DownloadException {
    try {//from   ww w  .  j  av  a2s .  c  om
        HttpClient httpclient = new DefaultHttpClient();
        //HttpHost proxy = new HttpHost("httppxlyon1.srv.volvo.com", 8080);
        //httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("Accept-Encoding", "gzip");

        HttpResponse response;

        response = httpclient.execute(httpget);
        LOGGER.debug(response.getStatusLine().toString());

        HttpEntity entity = response.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                LOGGER.debug("Accepting gzip for url : " + url);
                instream = new GZIPInputStream(instream);
            }

            return instream;
        }
    } catch (ClientProtocolException e) {
        throw new DownloadException(e);
    } catch (IOException e) {
        throw new DownloadException(e);
    }

    return null;
}