wood burning stoves*
The moose likes Java in General and the fly likes Retrieving a compressed file from a URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Retrieving a compressed file from a URL" Watch "Retrieving a compressed file from a URL" New topic
Author

Retrieving a compressed file from a URL

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm trying to access a URL that provides me with a ZIP file. But the second System.out.println below only gives me "zip_in_stream.available():1"



Then when I do uncompression, "temp.xml" file (the destination of the uncompression), it gives me nothing.



Any help would be greatly appreciated.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The fact that available() returns 1 doesn't mean anything - if you look at the source code for ZipInputStream, 0 and 1 are the only values that method can return. The available() method is rarely useful for anything, really.

Unfortunately I don't see where the problem is in your code. Does the "bytelength:" message appear at all?

One thing you might try doing is taking out the ZipInputStream, and try to copy the content to a file without uncompressing it. Then you can see if what con.getInputStream() is giving you is a valid zip file or not. (Just try opening it with another too, like Windows Explorer - or if you're on UNIX, the jar tool can work as well.) If nothing else, it will help narrow down where the problem is.

Ah, I think you probably need to use the getNextEntry() method to iterate through all entries in the file - even if there's just one. Remember, a zip file can contain multiple files within it. It should be something like this:


"I'm not back." - Bill Harding, Twister
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
(1.)
>>>>> ...... Does
>>>>> the "bytelength:" message appear at all?
No, it doesn't

(2.)
If I do this below, then I will get the byteLength and I will get the output in a file but the file is filled only with weird characters. I think because it's a zip file.


(3.)
And I can download and uncompress the zip file okay manually.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, I think there's a major mistake on this line:

while ( (byteLength = tempStream.read(input_buffer, 0, 1024)) > 0 )

That ought to be "zip_in_stream", there, not tempStream. My theory is that when you open the ZipInputStream, is asks the BufferedInputStream to read a little data to make sure this is a zip data stream. BufferedInputStream reads much more data than requested, and stores it internally; this is what a BIS does. Then when you try to read tempStream directly, most of the file data has already been read, and you're just getting the tail of the stream -- in fact, the "tail" may be zero-length, if the amount of data is less than the buffer size.

Make this one change, and see what happens!


[Jess in Action][AskingGoodQuestions]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I have tried "zip_in_stream". The "byteLength" message doesn't appear at all. And the destination output file is empty


[ January 15, 2008: Message edited by: Susan Smith ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I overlooked the use of tempStream - yes, that should definitely be zip_in_stream if you want to uncompress. However you still need to use the getNextEntry() method as I suggested in the last post. Otherwise the results are exactly as you describe: the ZipInputStream will appear to have no bytes to read.
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
Thanks Jim, Ernest

It's working great now.
 
GeeCON Prague 2014
 
subject: Retrieving a compressed file from a URL