Solved

read .gz file in java

Posted on 2010-08-20
6
1,163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

615 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