Solved

read .gz file in java

Posted on 2010-08-20
6
1,122 Views
Last Modified: 2013-11-21
Hi:

I need to download a .gz file online. Inside the gzip file is one csv file. All is well except that I am only getting first line (the table head) of the file.  Here's the code, please help:

String url  = new URL ("myurl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Cookie",cookielist.toString());
con.getContent();
                                    
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
GZIPInputStream gzin = new GZIPInputStream(in);
BufferedReader br = new BufferedReader(new InputStreamReader(gzin));
while ((line=br.readLine())!=null) {
     System.out.println("read: " + line);
}
0
Comment
Question by:sdc248
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33488414
>>con.getContent();

You should get rid of that. Is this url publicly available?
0
 

Author Comment

by:sdc248
ID: 33488514
With or without that line doesn't seem to make a difference.

The url is a password protected https web page. My code manages to login and read the html content of the web page where the file is published.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33488534
Make sure you're not ignoring any exceptions.
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 10

Accepted Solution

by:
Hegemon earned 250 total points
ID: 33488703
- My code manages to login and read the html content of the web page where the file is published

So html content or gzip content ?

If you manage to retrieve gzip and can see the first line of your csv file, then everything looks ok. If you are sure there is more than line, make sure the "lines" are understood equally on both platforms - where the file was created and where it is being downloaded to. I.e. if the fie was created on Unix and processed on Windows, the whole content can be interpreted as one long line simply because of the differences between just <LF> and <CR><LF>
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 33489093
>>If you are sure there is more than line, make sure the "lines" are understood equally on both platforms


Actually BR makes no distinction between platforms:

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/io/BufferedReader.html#readLine()
0
 

Author Comment

by:sdc248
ID: 33513825
I tried to have my code to download and read another .gz file on the same web page and it worked!

Looks like my code is fine. The mistery is on how the files were created, perhaps?

Anyway, thank you guys for your help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 127
simple java question 3 43
maven project error 5 44
factorial example 4 34
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now