使用Objective-zip的库读取zip文件,地址:http://code.google.com/p/objective-zip/
可以在iphone上进行运行.
简单的测试代码:
- #import <Foundation/Foundation.h>
- #import "Objective-Zip/ZipFile.h"
- #import "Objective-Zip/ZipException.h"
- #import "Objective-Zip/FileInZipInfo.h"
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- // insert code here...
- NSLog(@"Hello, World!");
- //初始化目录
- NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSString *filePath= [documentsDir stringByAppendingPathComponent:@"5.zip"];
- //此文件在zip包中,我们就读取此文件
- NSString *f = @"META-INF/container.xml";
- @try {
- ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];
- NSArray *infos= [unzipFile listFileInZipInfos];
- FileInZipInfo *currentInfo=Nil;
- //列出所有在zip中的文件信息
- for (FileInZipInfo *info in infos) {
- //@"- %@ %@ %d (%d)"
- NSString *fileInfo= [NSString stringWithFormat:@"%@", info.name, info.date, info.size, info.level];
- if([fileInfo isEqualToString:f]){
- NSLog(@"%@",fileInfo);
- }
- NSLog(@"%@",fileInfo);
- }
- //直接定位到要读取的文件
- if([unzipFile locateFileInZip:f]){
- //取得读取文件流
- ZipReadStream *read2= [unzipFile readCurrentFileInZip];
- currentInfo = [unzipFile getCurrentFileInZipInfo];
- //开始进行读取
- NSMutableData *data2= [[[NSMutableData alloc] initWithLength:currentInfo.length] autorelease];
- int bytesRead2= [read2 readDataWithBuffer:data2];
- NSLog(@"SIZE IS:%d,Read size is :%d",currentInfo.length,bytesRead2);
- NSString *fileText2= [[[NSString alloc] initWithBytes:[data2 bytes] length:bytesRead2 encoding:NSUTF8StringEncoding] autorelease];
- NSLog(@"%@",fileText2);
- }
- [unzipFile release];
- } @catch (ZipException *ze) {
- NSLog(@"ZipException caught: %d - %@", ze.error, [ze reason]);
- } @catch (id e) {
- NSLog(@"Exception caught: %@ - %@", [[e class] description], [e description]);
- }
- [pool drain];
- return 0;
- }