decompressing .gZ file from Document directory?

Posted by senthilmuthu on Stack Overflow See other posts from Stack Overflow or by senthilmuthu
Published on 2010-03-23T11:09:34Z Indexed on 2010/03/23 11:13 UTC
Read the original article Hit count: 397

Filed under:
|

hi, i am having .gZ (zip file) in document directory.i want unZip it.but i am using libz.dylib framework .will it decompress and save all data to that file path?how can i get that extracted data?any has experienced in doing this?any help?when i use the method,but when i put break point, it returns data error(used in NSLog)--Z_DATA_ERROR--

- (id)initWithGzippedData: (NSData *)gzippedData;
{
[gzippedData retain];
if ([gzippedData length] == 0) return nil;

unsigned full_length = [gzippedData length];
unsigned half_length = [gzippedData length] / 2;

NSMutableData *decompressed = [[NSMutableData alloc] initWithLength:(full_length + half_length)];
BOOL done = NO;
int status;

z_stream strm;
strm.next_in = (Bytef *)[gzippedData bytes];
strm.avail_in = [gzippedData length];
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;

 if (inflateInit2(&strm, (15+32)) != Z_OK) 
 {
    [gzippedData release];
    [decompressed release];
    return nil;
 }
 while (!done)
 {
    // Make sure we have enough room and reset the lengths.
    if (strm.total_out >= [decompressed length])
        [decompressed increaseLengthBy: half_length];
    strm.next_out = [decompressed mutableBytes] + strm.total_out;
    strm.avail_out = [decompressed length] - strm.total_out;

    // Inflate another chunk.
    status = inflate (&strm, Z_SYNC_FLUSH);
    if(status == Z_DATA_ERROR)
    {
        NSLog(@"data error");
    }
    if (status == Z_STREAM_END) 
        done = YES;
    else if (status != Z_OK) break;
 }
 if (inflateEnd (&strm) != Z_OK) 
 {
    [decompressed release];
    return nil;
 }

// Set real length.
 [decompressed setLength: strm.total_out];
 id newObject = [self initWithBytes:[decompressed bytes] length:[decompressed length]];
 [decompressed release];
 [gzippedData release];
 return newObject;

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk