Cocoa的Zip归档读写框架
作者: Cocoa China 【最热的Mac, iPhone专业开发论坛】【本文永久地址】
18
Jun
介绍一个zip读写框架:zip-framework。这个框架支持直接在程序中读写zip归档中的文件,而无需使用NSTask去执行命令行的unzip。
Cocoa并没有提供读写zip的功能(有GZIP: /usr/include/zlib.h,但是很有局限性),这个zip框架很好地实现了这一功能。这个框架使用Objective-C写成,因此可以非常方便地在程序中调用。理论上来讲也完全可以用于iPhone(如果谁有兴趣可以试一下)。
使用方法:
-
#import <stdio .h>
-
#import <zip /ZipArchive.h>
-
-
ZipArchive *zip = [[ZipArchive alloc] initWithFile:@"…"];
-
if (!zip) {
-
NSLog(@"File could not be opened");
-
}
-
-
FILE *fp = [zip entryNamed:@"README.txt"]; // open stream to file README.txt in archive
-
if (!fp) {
-
NSLog(@"Not a file or not available in the archive");
-
}
-
-
NSArray *allEntries = [zip entries];
-
// for example: [@"README", @"COPYING", @"src/", @"src/main.c"]
-
-
[zip release];
-
-
// Autoreleased version
-
ZipArchive *autoreleasedZip = [ZipArchive archiveWithFile:@"…"];
-
</zip></stdio>
这样就可以获取到zip包中的文件指针,可以直接对其进行读操作。(目前只支持fread操作)
下载地址位于:http://code.google.com/p/zip-framework/
发表评论