IT Certificaions

Google Cloud Platform

Friday, July 5, 2013

Read gzip response from a URL using Java

Here is a code template.....

public static String readGZFile(String url) throws IOException {
           StringBuffer dataBuffer =new StringBuffer();
           InputStream gzipStream = new GZIPInputStream((new URL(url)).openStream());
           Reader decoder = new InputStreamReader(gzipStream, "UTF-8");
           BufferedReader buffered = new BufferedReader(decoder);
           int i=0;
           String line=buffered.readLine();
           while(line !=null){
               dataBuffer.append(line);
               line=buffered.readLine();
               i++;
           }       
           return dataBuffer.toString();          
    }