Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ZLib Inflate of Arbitrary Length Output?

807598Aug 24 2006 — edited Aug 24 2006
Greetings. I have a question regarding java.util.zip.Inflater, used for zlib.
The class seems pretty straightforward, but I have trouble creating the output buffer because it is unknown size. Now, before you go yelling at me to go find out the size or for not keeping track of the size, let me give you a little background info. I'm working on a project opening files that a previous programmer left over. You can yell at him all you want, but he seems to have failed to record the output size - but was nice enough to include the input size.

So my question is simply, is there any way to Inflate a zlib into an arbitrary-length buffer? Maybe there's some method or something to find the size, or could I perhaps use a resizable data structure, or anything along those lines? I tried taking the Input buffer and multiplying it by 12, but sometimes zlib compression works so well that the resulting file is 1/100th the size of the original, and the output file becomes corrupted. I don't really want to use 100x, because then I can have a lot of leftover bytes, or it might still be too small.

Here's the code that I'm using so far, where b is an array of bytes (the input), and length is the size of the array. The end result is then stored in the array of bytes result.
Inflater decompresser2 = new Inflater();
decompresser2.setInput(b);

byte[] result = new byte[length * 12];
decompresser2.inflate(result);
int totalsize = decompresser2.getTotalOut();
decompresser2.end();

Inflater decompresser = new Inflater();
decompresser.setInput(b);
result = new byte[totalsize];
decompresser.inflate(result);
decompresser.end();
First, I do a dummy uncompress to 12 times the original size (and cross my fingers hoping that it didn't compress better than 12x) to find out the output size. I then uncompress again, this time to the actual size.

Message was edited by:
IsmAvatar
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2006
Added on Aug 24 2006
2 comments
672 views