Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation NSData (KDataCompressionCategory)
- - (NSData *)zlibInflate
- {
- if ([self length] == 0)
- return self;
- z_stream strm;
- strm.next_in = (Bytef *)[self bytes];
- strm.avail_in = (uInt)[self length];
- strm.total_out = 0;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- if (Z_OK != inflateInit(&strm))
- {
- NSLog(@"%s: inflateInit() failed!\n", __PRETTY_FUNCTION__);
- return nil;
- }
- NSMutableData *outData = [[[NSMutableData alloc] init] autorelease];
- Bytef tmpBuf[KZLibInflateBufferSize] = {0};
- if (!outData)
- return nil;
- int status = Z_OK;
- BOOL bDone = NO;
- while (!bDone)
- {
- strm.next_out = tmpBuf;
- strm.avail_out = KZLibInflateBufferSize;
- status = inflate(&strm, Z_SYNC_FLUSH);
- switch (status)
- {
- case Z_OK:
- case Z_STREAM_END:
- {
- // Process this block
- [outData appendBytes:tmpBuf length:(KZLibInflateBufferSize - strm.avail_out)];
- if (Z_STREAM_END == status)
- bDone = YES;
- break;
- }
- default:
- {
- NSLog(@"%s: ZLib inflate() failed with status %d!\n", __PRETTY_FUNCTION__, status);
- return nil;
- }
- }
- }
- if (Z_OK != inflateEnd(&strm))
- return nil;
- if (bDone)
- {
- return [[NSData dataWithData:outData] autorelease];
- }
- else
- {
- return nil;
- }
- }
- @end
Add Comment
Please, Sign In to add comment