ZLib Inflate of Arbitrary Length Output?

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, whereb is an array of bytes (the input), andlength is the size of the array. The end result is then stored in the array of bytesresult.

Inflater decompresser2 =new Inflater();

decompresser2.setInput(b);

byte[] result =newbyte[length * 12];

decompresser2.inflate(result);

int totalsize = decompresser2.getTotalOut();

decompresser2.end();

Inflater decompresser =new Inflater();

decompresser.setInput(b);

result =newbyte[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

[2269 byte] By [IsmAvatara] at [2007-10-3 3:29:04]
# 1

The Java doc for Inflater and Deflater are(is?) remarkably poor but after some experimentation I have

import java.io.*;

import java.util.zip.*;

public class Fred107

{

public static void main(String[] args) throws Exception

{

// Encode a String into bytes

String inputString = "blahblahblah? foiph asdi fhaisfh assi hsohiuhdfas foi;sdhfioashiashdfashdfaksjdhfpisshfiadshf ishfikds haidshia";

byte[] input = inputString.getBytes("UTF-8");

// Compress the bytes

Deflater compresser = new Deflater();

compresser.setInput(input);

compresser.finish();

byte[] buffer = new byte[7];

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while (!compresser.finished())

{

int len = compresser.deflate(buffer);

baos.write(buffer, 0, len);

}

baos.close();

// Decompress the bytes

Inflater decompresser = new Inflater();

byte[] compressedData = baos.toByteArray();

decompresser.setInput(compressedData, 0, compressedData.length);

byte[] result = new byte[5];

ByteArrayOutputStream baos1 = new ByteArrayOutputStream();

while (!decompresser.finished())

{

int len = decompresser.inflate(result);

baos1.write(result, 0, len);

}

decompresser.end();

// Decode the bytes into a String

String outputString = new String(baos1.toByteArray(), "UTF-8");

System.out.println(outputString);

}

}

sabre150a at 2007-7-14 21:22:48 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks a bunch. That's along the same lines as the way I did it in Pascal, but I had trouble translating the functionsThanks again.
IsmAvatara at 2007-7-14 21:22:48 > top of Java-index,Java Essentials,New To Java...