You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Cheok Yan Cheng <yc...@yahoo.com> on 2007/04/25 17:28:35 UTC

read and decompress gz file

Hello all,

I try to download a gz file, which contains a text
file. I try to use GZIPInputStream, to unzip from the
input stream of HttpMethod, but get a corrupted
result.

First, I suspect that I am doing something wrong with
GZIPInputStream. Hence, I try to confirm
GZIPInputStream is doing a right thing, by

(1) manual download the gz file from web browser and
save it to a disk.
(2) use GZIPInputStream to read FileInputStream, which
is point to the gz file.
(3) verify the output is correct.

Hence, I am suspect that I am doing something wrong
with HttpClient and get incorrect InputStream from it.

The following is my code :

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import
org.apache.commons.httpclient.params.HttpMethodParams;

import java.net.*;
import java.util.*;
import java.io.*;
import java.util.zip.*;

/**
 *
 * @author yccheok
 */
public class Test {
    
    /** Creates a new instance of Test */
    public Test() {
    }
    
    public static final void main(String[] args)
throws Exception {
        HttpClient httpClient = new HttpClient();
        
        byte data[] = new byte[1024];
        HttpMethod method = new
GetMethod("http://yancheng.cheok.googlepages.com/1295.dat.gz");
        StringBuffer s = new
StringBuffer(data.length);
            
        httpClient.executeMethod(method);
        InputStream inputStream =
method.getResponseBodyAsStream();
        java.util.zip.GZIPInputStream gZipInputStream
= new java.util.zip.GZIPInputStream(inputStream);

        while(gZipInputStream.read(data, 0,
data.length) != -1) {
            s.append(new String(data));
        }

        gZipInputStream.close();
        
        System.out.println("" + s);
        System.out.flush();
                
        /*
         *However, if I download manually and use
GZIPInputStream to extract read
         *the file, with no HttpClient involvement, no
problem.
         *
        // Open the gzip file
        String inFilename =
"/home/yccheok/Desktop/1295.dat.gz";
        GZIPInputStream gzipInputStream =
        new GZIPInputStream(new
FileInputStream(inFilename));
        // Open the output file
        String outFilename =
"/home/yccheok/Desktop/output.txt";
        OutputStream out = new
FileOutputStream(outFilename);


        // Transfer bytes from the compressed file to
the output file
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0)
{
        out.write(buf, 0, len);
        }


        // Close the file and stream
        gzipInputStream.close();
        out.close();
        */
        
        
        
        /*
        try {
            StockHistoryServer server = new
CIMBStockHistoryServer("1295");
        }
        catch(StockHistoryNotFoundException exp) {
            exp.printStackTrace();
        }
        */
    }
}

Is there anything I had missed out? Thank you very
much!

cheok

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: read and decompress gz file

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Cheok,

> I try to download a gz file, which contains a text
> file. I try to use GZIPInputStream, to unzip from the
> input stream of HttpMethod, but get a corrupted
> result.

>         while(gZipInputStream.read(data, 0,
> data.length) != -1) {
>             s.append(new String(data));
>         }

Why do you convert binary data to characters here?

>         /*
>          *However, if I download manually and use
> GZIPInputStream to extract read
>          *the file, with no HttpClient involvement, no
> problem.
>          *
>         // Transfer bytes from the compressed file to
> the output file
>         byte[] buf = new byte[1024];
>         int len;
>         while ((len = gzipInputStream.read(buf)) > 0)
> {
>         out.write(buf, 0, len);
>         }

There is no binary to character conversion here.

hope that helps,
  Roland