Objective c 压缩/解压缩内存中的字符串

Objective c 压缩/解压缩内存中的字符串,objective-c,ios,compression,Objective C,Ios,Compression,有人能给我提供一个关于objective-c(用于iPhone开发)中压缩和解压缩内存中字符串的教程/文档吗 我正在研究Objective Zip,但它似乎只在将压缩数据写入文件时才起作用。举个例子 @interface NSString (Gzip) - (NSData *)compress; @end @implementation NSString (Gzip) - (NSData *)compress { size_t len = [self length]; s

有人能给我提供一个关于objective-c(用于iPhone开发)中压缩和解压缩内存中字符串的教程/文档吗

我正在研究Objective Zip,但它似乎只在将压缩数据写入文件时才起作用。

举个例子

@interface NSString (Gzip)
- (NSData *)compress;
@end



@implementation NSString (Gzip)

- (NSData *)compress
{
    size_t len = [self length];
    size_t bufLen = (len + 12) * 1.001;
    u_char *buf = (u_char *)malloc(bufLen);
    if (buf == NULL) {
        NSLog(@"malloc error");
        return nil;
    }
    int err = compress(buf, &bufLen, (u_char *)[[self dataUsingEncoding:NSUTF8StringEncoding] bytes], len);
    if (err != Z_OK) {
        NSLog(@"compress error");
        free(buf);
        return nil;
    }

    NSData *rtn = [[[NSData alloc] initWithBytes:buf length:bufLen] autorelease];
    free(buf);

    return rtn;
}


@end
如果想使用zlib,请参见。如果想使用bzlib,也请参见。