Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Zipping an inputstream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Zipping an inputstream" Watch "Zipping an inputstream" New topic
Author

Zipping an inputstream

D. Nguyen
Greenhorn

Joined: Jun 09, 2006
Posts: 5
Hi,

I've got an inputstream which I want to zip. But the results has to be an inputstream also. Is this possible?
Because if I understood some tutorials correctly, you will get an outputstream if you zip an inputstream (for example: a file).

Thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Welcome to JavaRanch.

You can construct a ZipInputStream from an InputStream, and the result is still an InputStream (because ZipInputStream extends InputStream).

But ZipInputStream is meant for unzipping things; for zipping you'd use a ZipOutputStream. What exactly are you trying to do?


Ping & DNS - my free Android networking tools app
D. Nguyen
Greenhorn

Joined: Jun 09, 2006
Posts: 5
Thanks for your reply.

I have an Inputstream, more exactly a FileInputStream. I need to zip this stream, but the result has to be an inputstream also.

(the file has to be send by network, that's why I want to zip it, so the program has to send less bytes).

I hope you will understand my question better now?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Then maybe a GZipInputStream is what you are looking for. Or if you are copying the InputStream to an OutputStream, copy it to a GZipOutputStream that wraps that OutputStream instead.
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
I'll take a stab...

You want to compress the input stream coming in and pass it along as an InputStream? I'm thinking of something like a stream filter. Look at the piped I/O stream classes. Maybe you can construct a ZipOutputStream around a piped outputstream that's connect to a piped input stream that you pass along. The problem here is you get into Threads because you would want to write or convert the original InputStream in a separate thread that you start and then pass the piped input stream along.

That's off of my head and there are some problems with the code no doubt. For one you'd have to know who's responsible for flushing and closing the streams. But the basic idea is you put the logic to read from the original stream and write to the Zipoutput stream (constructed around the pipedOutput) in the ZipStreamConverter class that I didn't show. (Exercise left to the reader.) Am I close to what you wanted?

The alternative approach is to create a custom input stream that wraps an InputStream and implements the zip compression algorithm as it reads from the wrapped stream. That logic would either be inlined or delegated to some class that I don't think exists in the JDK. In other words you'd probably have to implement the compression logic. I'm sure there's an object you can talk to directly but I'm not clever enough to find it.
[ June 09, 2006: Message edited by: Clifton Craig ]

Holla at me...<br /><a href="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
D. Nguyen
Greenhorn

Joined: Jun 09, 2006
Posts: 5
Thank you Clifton Craig. As I expected there's no easy solution, but what you described is exactly what I need. I will try your first approach first. I already have something similar in my program, using piped input/outputstream to turn an outputstream to an inputstream, but I was wondering if there wasn't an easy solution.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
As I understand the zip algorithm the whole idea is doomed to failure from the start as it depends on knowing all the data before writing the output.


42
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

I didn't just pick GZipOutputStream because it looked good in the API docs. I picked it because I had read this article which described a servlet filter that automatically compressed responses, and it sounded a lot like your requirements.

Edit: On the other hand, if you don't just need compressed data, you need data that looks like a file in the ZIP format, then I think you are condemned to using one of those complex solutions.
[ June 09, 2006: Message edited by: Paul Clapham ]
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
D. Nguyen,

I'm glad I could help. I still wonder if there's an easy way to apply the Zip algorithm without fiddling with threads. One idea off my mind would be to try to buffer x amount of bytes in an InputStreamZipFilter class, push a block of data from the wrapped InputStream through a ZipOutputStream instance that writes to a ByteArrayInputStream instance that feeds the buffer and as bytes are requested from the caller return the converted bytes from the byte array buffer. The problem there would be determining how big the byte array should be vs. how big the block size on each read should be when the buffer is exhausted. It would all be experimental and need to be toyed with to get it right. There's gotta be a better way to do it though.
 
Don't get me started about those stupid light bulbs.
 
subject: Zipping an inputstream