Reputation: 10245
I am trying to decompress some zlib encoded data that I am downloading using ASIHTTPRequest wrapper.
This is what I have so far.. pretty much just getting the data in and now I was hoping for some help to start the decompression process.
- (IBAction)sendHttpsRequest
{
//Set request address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"https://192.168.17.36:443"];
//call ASIHTTP delegates (Used to connect to database)
NSURL *url = [NSURL URLWithString:databaseURL];
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setValidatesSecureCertificate:NO];
[request setPostValue:@"ClientDataSet.xml" forKey:@"filename"];
[request startSynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
responseData = [request responseData]; //Pass requested data from server over to NSData variable..
//Is this where I have to think about decompression? or do I pass this data out to another method?
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
self.hudCheck = NO;
[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(stopAnimating) userInfo:nil repeats:NO];
NSError *error = [request error];
NSLog(@"%@", error);
}
So pretty much I am up to the decompression stage however I am not sure which functions/methods etc I need to use to decompress this data. I have only ever attempted this with gzip which was comparatively easy as the ASI wrapper handles everything itself.
However due to circumstances outside my control the data I am now getting is zlib encoded.. So any functions, methods or examples you can tell me about that will help my cause would be greatly appreciated.
Edit:
Moving forward from @Caleb response I have added
include "zlib.h" to my file and now have access to this frameworks methods. One of which is the inflate() method.. however I am not sure what parameters need to be entered into it? any ideas?.
Upvotes: 1
Views: 5405
Reputation: 604
See http://iosdeveloperzone.com/2013/03/14/code-snippet-decompressing-a-gzipped-buffer/
This is a great example of zlib.h for iOS. Very clear and concise. No 3rd party library required.
Upvotes: 0
Reputation: 39512
Check out this NSData category: http://www.cocoadev.com/index.pl?NSDataCategory
Using this you can simply write:
uncompressedResponseData = [[request responseData] zlibInflate];
Upvotes: 1
Reputation: 124997
Have you had a look at the example on the zlib web site? inflateInit()
, inflate()
, and inflateEnd()
look like (almost) all you need.
You'll want to apply the lesson from the example in your -requestFinished:
method. In other words, you'll first download all the compressed data, and only then try to decompress it. It's probably possible to decompress as you download, but that'll be more complicated without being more helpful.
Update: To answer your follow-up question, the two parameters you need to pass into inflate()
are the z_stream (which you will have initialized previously using inflateInit()
) and a flush option. The example uses Z_NO_FLUSH -- you'll have to read a bit to find out what other options are possible.
My advice is to take the inf()
function from the example, minus all the interspersed explanatory text, and paste it into your code. Next, convert it to read from your NSData and write to an NSMutableData, possibly using NSInputStream and NSOutputStream as suggested in my comment below. That should give you a function to which you can pass a NSData containing compressed data and which will return a NSMutableData containing the uncompressed data.
Upvotes: 3