论坛首页 移动开发技术论坛

使用Objetive-zip读取zip文件

浏览 3928 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-08-10  
iOS

版权所有,欢迎转载,转载请注明 : 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;
}
 
   发表时间:2010-08-21  
最近想学习IPHONE开发了,怎么样发现IPHONE开发的贴子这么少啊?为啥呢?
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics