I am trying to decompress some zlib data using the dataByDecompressingData: method from ObjectiveZlib example code, which I have had some help converting to be used with Objective-c instead of c++. Code below
- (NSData*) dataByDecompressingData:(NSData*)data{
Byte* bytes = (Byte*)[data bytes];
NSInteger len = [data length];
NSMutableData *decompressedData = [[NSMutableData alloc] initWithCapacity:COMPRESSION_BLOCK];
Byte* decompressedBytes = (Byte*) malloc(COMPRESSION_BLOCK);
z_stream stream;
int err;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
stream.next_in = bytes;
err = inflateInit(&stream);
CHECK_ERR(err, @"inflateInit");
while (true) {
stream.avail_in = len - stream.total_in;
stream.next_out = decompressedBytes;
stream.avail_out = COMPRESSION_BLOCK;
err = inflate(&stream, Z_NO_FLUSH);
[decompressedData appendBytes:decompressedBytes length:(stream.total_out-[decompressedData length])];
if(err == Z_STREAM_END)
break;
CHECK_ERR(err, @"inflate");
}
err = inflateEnd(&stream);
CHECK_ERR(err, @"inflateEnd");
free(decompressedBytes);
return decompressedData;
}
Once this runs I then get the Z_BUF_ERROR, I have read here which I know is referring to ASIHTTPReqest but I figure might be using the same zlib classes supplied by xcode to decompress, and they said its an error with the size of the Buffer not being able to handle the decompression of the file due to space.
I am not totally sure if this is correct, they offered two solutions to fix, the first would be to split the packet… thats not an option imo, the other thing to do would be to increase the buffer size.. I was wondering A, how could I do this? B, is there a better third option?
any help would be greatly appreciated.
Please refer to http://www.zlib.net/zlib_how.html
Some of the errors I see:
1/ everytime you set
stream.next_in, you should setstream.avail_in, too. Note that you shouldn’t changestream.avail_inwhen you are not giving next chunk of input. Setting the initialavail_inbefore callinginflateInitmight be a good idea.2/ Using
inflateInit2(&stream, -MAX_WBITS)might be a good idea. Note that the normal version ofinflateInitdoesn’t check data for compression type (gzip or zlib) and if it chooses gzip, your decompression will fail.(should work, written without testing)