Search code examples
iphonesoapxcode4compression

unzip SOAP message with xcode


In order to improve the client/server behaviour, I'm looking for adapt my iphone client code to proceed ziped responses.

The server adapt the SOAP response ziped.

I was looking how to uncompress the response but didn't work for me.

The first solution I studied was the ZipArchive, explained here, solution (from minizip) but it is focus on filesystem compression.

And I just need to uncompress a NSString.

After that I checked this second approach:

NSData *decodedGzippedData = [NSData dataFromBase64String:encodedGzippedString];
NSData* unGzippedJsonData = [ASIHTTPRequest uncompressZippedData:decodedGzippedData];   
NSString* unGzippedJsonString = [[NSString alloc] initWithData:unGzippedJsonData encoding:NSASCIIStringEncoding];       

But didn't work for me, because in the actual version the NSData dataFromBase64String didn't exists.

Now I'm working with the third response of the previous question, anybody knows which library or framework I need to install in order to import Base64.h and NSData+Compression.h ¿? Used in this other potencial solution


Solution

  • The solution was the next.

    Install the next libraries to your project:

    • Base64.h // You can find it here
    • NSData+Compression.h // You can find it here

    Use the code of one of the previous solutions

    #import "Base64.h"
    #import "NSData+compression.h"
    
    ...
    
    // decoding the base64 ziped message
    Byte inputData[[stringValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
    [[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];
    size_t inputDataSize = (size_t)[stringValue length];
    size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);
    Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data
    Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);
    
    // inflate the original string using gzip
    NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize];
    NSData* result = [theData gzipInflate];//make bigger==gunzip
    
    // Return the result
    return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];