Q:

How do I decompress an NSString I have that was compressed with zlib

I have an NSString (e.g. eNoLycgsVgChvILSEoWS1IoSPQBFZgb4) that I get from a web server that is just a normal string compressed using zlib. How do I decompress this in Objective-C? I've found some libraries but they only accept full files, not a simple string.

A:

OK based on the above it seems you didn't really know what the format is - but seems that it's ZLib deflate, Base64 encoded. Cribbing shamelessly off other StackOverflow answers, here's a chunk of code that'll decompress your data:
#import <Foundation/Foundation.h>
#import <zlib.h>

int main( int argc, const char * argv[] )
{
    @autoreleasepool
    {
        NSLog( @"Starting..." );

        NSString      * base64String     = @"eJwLycgsVgChvILSEoWS1IoSPQBFZgb4";
        NSData        * compressedData   = [ [ NSData alloc ] initWithBase64EncodedString: base64String options: 0 ];
        NSUInteger      full_length      = [ compressedData length ];
        NSUInteger      half_length      = [ compressedData length ] / 2;
        NSMutableData * decompressedData = [ NSMutableData dataWithLength: full_length + half_length ];
        BOOL            done             = NO;
        int             status;
        z_stream        strm;

        strm.next_in   = ( Bytef * )[ compressedData bytes  ];
        strm.avail_in  = ( unsigned int ) full_length;
        strm.total_out = 0;
        strm.zalloc    = Z_NULL;
        strm.zfree     = Z_NULL;

        if ( inflateInit2( &strm, ( 15 + 32 ) ) != Z_OK )
        {
            NSLog( @"Could not initialise ZLib" );
            return EXIT_FAILURE;
        }

        while ( done == NO )
        {
            // Make sure we have enough room and reset the lengths.

            if ( strm.total_out >= [ decompressedData length ] )
            {
                [ decompressedData increaseLengthBy: half_length ];
            }

            strm.next_out  = [ decompressedData mutableBytes ] + strm.total_out;
            strm.avail_out = ( unsigned int )( [ decompressedData length ] - strm.total_out );

            // Inflate another chunk.

            status = inflate( &strm, Z_SYNC_FLUSH );

            if ( status == Z_STREAM_END )
            {
                done = YES;
            }
            else if ( status != Z_OK )
            {
                NSLog( @"Decompression failed with status %i", status );
                break;
            }
        }

        if ( inflateEnd( &strm ) != Z_OK )
        {
            NSLog( @"Could not complete decompression" );
            return EXIT_FAILURE;
        }

        // Set real length.

        if ( done )
        {
            [ decompressedData setLength: strm.total_out ];

            NSString * string = [ [ NSString alloc ] initWithData: decompressedData
                                                         encoding: NSUTF8StringEncoding ];

            NSLog( @"String: %@", string );
        }
        else
        {
            return EXIT_FAILURE;
        }
    }

    NSLog( @"...Success" );

    return EXIT_SUCCESS;
}

When run, this prints something like:
2016-06-16 12:40:14.454 Objective Z[82569:7532295] Starting...
2016-06-16 12:40:14.455 Objective Z[82569:7532295] String: This is input text.
2016-06-16 12:40:14.455 Objective Z[82569:7532295] ...Success
Program ended with exit code: 0

You'll need to ensure you link against ZLib. From your project's Build Phases section in XCode, under "Link Binary With Libraries", the "+" button gives a searchable popup; look for "libz". This might not be present on iOS; it is present in the OS X SDK; that's beyond scope of this answer and Google will doubtless present an answer swiftly if you have trouble.

