• Post Reply Bookmark Topic Watch Topic
  • New Topic

Download zip file using Java  RSS feed

 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to download a zip file using java i.e it is a HTTP GET request not POST.

When I unzip the files using unzip on Ubuntu I get a message saying that the zip file is corrupted. I have no problems downloading and unzipping using the browser.

This is the code I am using to download. Can someone guide me on downloading an uncorrupted zip file using Java ? I tried to remove the content type request property but had the same difficulties.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try looking what the file that you downloaded actually contains?

Maybe it's a HTML page containing an error message instead of an actual ZIP file.

One thing that looks suspicious to me is the content type that you set. As far as I know, "binary/data" is not a valid content type. It could be that the server is reporting an "invalid content type" error.

For arbitrary binary data, use "application/octet-stream" instead of "binary/data". Or use "application/zip", which is the official type for ZIP files.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My file contains binary data. I can read the binary data just fine if I download from the browser. First I unzip it and then I run a stand alone program. It works fine.

I will try using application/zip and report back to you.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:My file contains binary data. I can read the binary data just fine if I download from the browser. First I unzip it and then I run a stand alone program. It works fine.

Yes, but you can't just invent your own content type such as "binary/data" and expect it to work. Content types have specific meanings, and if you tell the server "I want the data as 'binary/data'" the server is going to tell you "I don't know what you mean by 'binary/data'". You have to use one of the standard content types, that the server understands.

When you download the file with your browser instead of a Java program, the browser will also send a 'Content-Type' header to the server, containing a standard content type.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper point taken. I will get back with this change you have suggested. Now that you mentioned it I looked up all the MIME types that have been defined.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:My file contains binary data.
If your talking about the zip file on the server, I would assume it's binary. If you are talking about the file that you downloaded with your code, then no, I would not make the assumption that the file is binary, even if the file name has a .zip suffix. You could open it up with VI (or some other editor) and look to see if it isn't perhaps an html file.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey - I know for sure that the zip files do contain binary files. I hve unzipped and verified it.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That should be '-1' not '0'.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper addition of application/zip did not have any impact. The zip file is still corrupted. Would I have to add the user-agent and specify that as java/1.8_0_45 ?
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seriously.
-1, not 0.
The first time in your reading loop that you get 0 bytes returned you will exit...which will be before the stream has finished (EOS is marked by a -1 returned), so you lose a load of data from the end of your zip file.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave - did that already -
That code now reads -
while ((count = inputs.read(b)) > -1)

The problem still persists.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you see if you open the corrupt ZIP file with a text editor?

Do you see an unreadable mess of characters, or does it look like an HTML page with an error message from the server?

Sometimes, servers return an HTML page with error information if something is wrong. You have then saved that HTML page in your ZIP file.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper you are absolutely right. When I open the zip file in emacs I get an HTML page but it is not really an error page . It is a listing of all the directories where the individual zip files are located. How do I fix this ?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suraj sahitya wrote:Carey - I know for sure that the zip files do contain binary files. I hve unzipped and verified it.
How could you have unzipped it when you say it was corrupted?
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey I unzipped the one that I downloaded from the browser. So that was uncorrupted.

Anyway problem fixed thanks to Jesper. The HTML output of the zip file showed me that my input for the download had a problem and the code is fixed and downloads are happening !
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies but this problem is still unresolved !

The download did happen and I can unzip the file using unzip on Ubuntu. No errors. However when I open the file in a text editor I get this message -

M Filemode Length Date Time File
- ---------- -------- ----------- -------- -----------
-rw-r--r-- 2884802 15-Jan-2009 10:07:40 N06E072.hgt
- ---------- -------- ----------- -------- -----------
2884802 1 file

And when I open the file using the command more N06E072.hgt I do not get binary data but blanks. When I read this file using Java I get all zero values for this binary file. But this file has all non zero values. It appears the download is not retrieving the right data. The file size after unzipping is right.
It is indeed 2884802 bytes. But the data is incorrect.
 
suraj sahitya
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. My code works ! Thanks to all who helped me download zip files. Finally I can avoid using the browser and do bulk downloads !!!

Many thanks to Jesper and others for guiding me !!!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!