Solved

read .gz file in java

Posted on 2010-08-20
6
1,128 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 73
jar file executable 12 52
setup wamp server for first time 2 72
diffSum example 4 37
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 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