iPhone开发 使用Objetive-zip读取zip文件
2010-08-26 00:08:00 来源:WEB开发网使用Objective-zip的库读取zip文件,地址:http://code.google.com/p/objective-zip/
可以在iphone上进行运行.
简单的测试代码:
Cpp代码
#import
#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);
更多精彩
赞助商链接