0

I am unable to unzip a zip file saved in Sqlite As Blob , after i query the db it returns me a NSData . I tried the inflate method but it returns this error "Decompression of data failed with code -3". Any opinions??

Thanks !

This is the code

    NSData *content = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 3) length:sqlite3_column_bytes(statement, 3)];
   content = [NSData gzipInflate:content];

/// Ns data category

@implementation NSData (Unzip)


+ (NSData *)gzipInflate:(NSData*)data
{
    if ([data length] == 0) return data;

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

    NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
    BOOL done = NO;
    int status;

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

    if (inflateInit2(&strm, (15+32)) != Z_OK) 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_STREAM_END) done = YES;
        else if (status != Z_OK) break;
    }
    if (inflateEnd (&strm) != Z_OK) return nil;

    // Set real length.
    if (done)
    {
        [decompressed setLength: strm.total_out];
        return [NSData dataWithData: decompressed];
    }
    else return nil;
}
@end
Daniel
  • 7
  • Did you follow this http://stackoverflow.com/questions/2624767/how-can-i-unzip-a-file-in-macos-x-obj-c – Hussain Shabbir Nov 15 '13 at 11:06
  • From sqlite i get a blob that is zipped so the question is how to unzip it and how to use those classes . I'm making an app for iPad – Daniel Nov 15 '13 at 15:39

0 Answers0