Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:49:53+00:00 2026-06-15T19:49:53+00:00

I am trying to decompress some zlib data using the dataByDecompressingData: method from ObjectiveZlib

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:49:54+00:00Added an answer on June 15, 2026 at 7:49 pm

    Please refer to http://www.zlib.net/zlib_how.html

    Z_BUF_ERROR will be explained further below, but suffice it to say that this is simply an indication that inflate() could not consume more input or produce more output. inflate() can be called again with more output space or more available input, which it will be in this code.

    Some of the errors I see:

    1/ everytime you set stream.next_in, you should set stream.avail_in, too. Note that you shouldn’t change stream.avail_in when you are not giving next chunk of input. Setting the initial avail_in before calling inflateInit might be a good idea.

    2/ Using inflateInit2(&stream, -MAX_WBITS) might be a good idea. Note that the normal version of inflateInit doesn’t check data for compression type (gzip or zlib) and if it chooses gzip, your decompression will fail.

    (should work, written without testing)

    -(NSData*)decompressData:(NSData*)compressedData {
        z_stream stream;
    
        stream.zalloc = Z_NULL;
        stream.zfree = Z_NULL;
        stream.opaque = Z_NULL;
    
        //we set the input, no need to change the values later
        stream.next_in = (Bytef *) [compressedData bytes];
        stream.avail_in = compressedData.length;  
    
        int result = inflateInit2(&stream, -MAX_WBITS);
    
        if (result != Z_OK) {
            return nil;
        }
    
        NSMutableData* uncompressedData = [NSMutableData data];
        //make buffer big enough - 128KB
        int bufferLength = 128 * 1024;
        char *buffer = malloc(bufferLength);
    
        while (true) {
            stream.next_out = buffer;
            stream.avail_out = bufferLength;
    
            //calling with Z_FINISH because we already have all the input
            result = inflate(&stream, Z_FINISH);
    
            if (result != Z_OK && result != Z_STREAM_END) {
                 inflateEnd(&stream);
                 free(buffer);
                 return nil;
            }
    
            [uncompressedData appendBytes:buffer length:(bufferLength - stream.avail_out)];
    
            if (result == Z_STREAM_END) {
                break;
            }
        }
    
    
        inflateEnd(&stream);
        free(buffer);
    
        return uncompressedData;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to uncompress some data created in VB6 using the zlib API.
I am currently trying to decompress some data that I have coming in with
I'm trying to write huffman decompress, I have a function which is trying to
I'm trying to decompress gzipped data with Inflater . According to the docs, If
I have the following code which we use to compress Strings (with error and
Hey, I'm trying to unzip a file using this class: public class Decompress {
I'm trying to compress HTML with JavaScript and decompress it with Ruby. Some carachters
I am trying to decompress some MMS messages sent to me zipped. The problem
I'm trying to decompress some large file sent by a server and then, after
I am trying to install CppUnit on Windows. I have downloaded it from here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.