如何在不直接使用zlib.dylib的情况下使用Zlib压缩数据?

时间:2009-12-17 22:19:42

标签: objective-c zlib

是否有一个类允许使用Zlib压缩数据,或者直接使用zlib.dylib是我唯一的可能性?

4 个答案:

答案 0 :(得分:11)

NSData + Compression是一个易于使用的NSData类别实现。

用法:

NSData* compressed = [myData zlibDeflate];
NSData* originalData = [compressed zlibInflate];

答案 1 :(得分:4)

答案 2 :(得分:1)

这对我有用: 1)基于ZLib的Objective-Zip新位置:https://github.com/gianlucabertani/Objective-Zip

Podfile:

pod 'objective-zip', '~> 1.0'

快速举例:

#import "ViewController.h"
#import "Objective-Zip.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];

    OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:path
                                                       mode:OZZipFileModeCreate];
    NSString *str = @"Hello world";
    OZZipWriteStream *stream= [zipFile writeFileInZipWithName:@"file.txt"
                                             compressionLevel:OZZipCompressionLevelBest];
    [stream writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
    [stream finishedWriting];
    [zipFile close];
}

2)其他基于zlib的库也运行良好。 https://github.com/ZipArchive/ZipArchive

注意:有时需要将libz.tbd(zlib.dylib的新名称)添加到“Link Binary With Libraries”

快速举例:

#import "SSZipArchive.h"
...
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSError *error;        
    NSString *str = @"Hello world";
    NSString *fileName = [docsDir stringByAppendingPathComponent:@"test.txt"];
    BOOL succeed = [str writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (succeed){
        NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"test.zip"]];
        [SSZipArchive createZipFileAtPath:path withFilesAtPaths:@[fileName]];
    }
}

答案 3 :(得分:0)

另外,还有objective-zip,它是“一个小的Cocoa / Objective-C库,它以面向对象的友好方式包装ZLib和MiniZip。”

将文件写入“.zip”存档很简单,因为执行以下代码:

ZipWriteStream *stream = [zipFile writeFileInZipWithName:@"abc.txt" compressionLevel:ZipCompressionLevelBest];
[stream writeData:abcData];
[stream finishedWriting];

该库还允许读取“.zip”文件的内容,并枚举它包含的文件。

列出“.zip”文件的内容是通过类似下面的代码完成的。

ZipFile *unzipFile = [[ZipFile alloc] initWithFileName:@"test.zip" mode:ZipFileModeUnzip];
NSArray *infos = [unzipFile listFileInZipInfos];

for (FileInZipInfo *info in infos) {
  NSLog(@"- %@ %@ %d (%d)", info.name, info.date, info.size, info.level);

  // Locate the file in the zip
  [unzipFile locateFileInZip:info.name];

  // Expand the file in memory
  ZipReadStream *read = [unzipFile readCurrentFileInZip];
  NSMutableData *data = [[NSMutableData alloc] initWithLength:256];
  int bytesRead = [read readDataWithBuffer:data];
  [read finishedReading];
}