1

Can anybody tell me how to decompress a byte array?

Here is my code. I have been trying it for ages and it is giving me a DataFormatException.

byte bArray[] = new byte[tSizeOfTile];
input.read(bArray, offset, limit);

byte[] unComp = new byte[bArray.length + 100];

Inflater inflate = new Inflater();
inflate.setInput(bArray);
inflate.inflate(unComp, offset, bArray.length + 100);
inflate.end();
1
  • From where come those data in bArray ? Commented Aug 16, 2010 at 10:38

1 Answer 1

5

Well, one definite issue is that you're apparently using InputStream.read without checking the return value. That means you may have read less data than you expected to.

Also, you're trying to inflate into unComp from the offset, but with a maximum length being the same as unComp.length. That means if offset is anything other than 0, you could be trying to write past the end of the array.

Did you mean the offset to refer to the input array? I don't believe that's what it means. You should use setInput to only provide input data.

However, you could make all of this a lot easier for yourself by using InflaterInputStream instead of handling Inflate yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

i have also used so many Streams like input stream, SwappedDataInputStream and CountingInputstream so i do not want to use any other stream. i just do not know why its giving me DataFormatExeption. even this file is written using Zlib and Inflater class also use Zlib. so there should not be any problem.
@sajjoo: Are you sure that the original data is actually valid? Might you have a bug in the code which is compressing it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.