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();
Well, one definite issue is that you’re apparently using
InputStream.readwithout checking the return value. That means you may have read less data than you expected to.Also, you’re trying to inflate into
unCompfrom the offset, but with a maximum length being the same asunComp.length. That means ifoffsetis 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
setInputto only provide input data.However, you could make all of this a lot easier for yourself by using
InflaterInputStreaminstead of handlingInflateyourself.