使用Objetive-zip读取zip文件
版权所有,欢迎转载,转载请注明 : SinFrancis ?http://mdev.cc?
?
使用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;}? 1 楼 xiao0556 2010-08-21 最近想学习IPHONE开发了,怎么样发现IPHONE开发的贴子这么少啊?为啥呢?