介绍一个zip读写框架:zip-framework。这个框架支持直接在程序中读写zip归档中的文件,而无需使用NSTask去执行命令行的unzip。

Cocoa并没有提供读写zip的功能(有GZIP: /usr/include/zlib.h,但是很有局限性),这个zip框架很好地实现了这一功能。这个框架使用Objective-C写成,因此可以非常方便地在程序中调用。理论上来讲也完全可以用于iPhone(如果谁有兴趣可以试一下)。

使用方法:

  1. #import <stdio .h>
  2. #import <zip /ZipArchive.h>
  3.  
  4. ZipArchive *zip = [[ZipArchive alloc] initWithFile:@"…"];
  5. if (!zip) {
  6.     NSLog(@"File could not be opened");
  7. }
  8.  
  9. FILE *fp = [zip entryNamed:@"README.txt"]; // open stream to file README.txt in archive
  10. if (!fp) {
  11.     NSLog(@"Not a file or not available in the archive");
  12. }
  13.  
  14. NSArray *allEntries = [zip entries];
  15. // for example: [@"README", @"COPYING", @"src/", @"src/main.c"]
  16.  
  17. [zip release];
  18.  
  19. // Autoreleased version
  20. ZipArchive *autoreleasedZip = [ZipArchive archiveWithFile:@"…"];
  21. </zip></stdio>

这样就可以获取到zip包中的文件指针,可以直接对其进行读操作。(目前只支持fread操作)

 

下载地址位于:http://code.google.com/p/zip-framework/