Solved

read .gz file in java

Posted on 2010-08-20
6
1,119 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
Comment Utility
>>con.getContent();

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

Author Comment

by:sdc248
Comment Utility
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
Comment Utility
Make sure you're not ignoring any exceptions.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Accepted Solution

by:
Hegemon earned 250 total points
Comment Utility
- 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
Comment Utility
>>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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

10 Experts available now in Live!

Get 1:1 Help Now