iPhone开发读取ZIP文件

使用Objective-zip的库读取zip文件,地址:http://code.google.com/p/objective-zip/

 

可以在iphone上进行运行.

 

简单的测试代码:

 

  1. #import <Foundation/Foundation.h>  
  2. #import "Objective-Zip/ZipFile.h"  
  3. #import "Objective-Zip/ZipException.h"  
  4. #import "Objective-Zip/FileInZipInfo.h"  
  5. int main (int argc, const char * argv[]) {  
  6.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  7.   
  8.     // insert code here...  
  9.     NSLog(@"Hello, World!");  
  10.       
  11.     //初始化目录  
  12.     NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  
  13.     NSString *filePath= [documentsDir stringByAppendingPathComponent:@"5.zip"];  
  14.     //此文件在zip包中,我们就读取此文件  
  15.     NSString *f = @"META-INF/container.xml";  
  16.       
  17.     @try {  
  18.       
  19.     ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:filePath mode:ZipFileModeUnzip];  
  20.     NSArray *infos= [unzipFile listFileInZipInfos];  
  21.           
  22.         FileInZipInfo *currentInfo=Nil;  
  23.           
  24.         //列出所有在zip中的文件信息  
  25.     for (FileInZipInfo *info in infos) {  
  26.         //@"- %@ %@ %d (%d)"  
  27.         NSString *fileInfo= [NSString stringWithFormat:@"%@", info.name, info.date, info.size, info.level];  
  28.         if([fileInfo isEqualToString:f]){  
  29.   
  30.             NSLog(@"%@",fileInfo);        
  31.       
  32.               
  33.         }  
  34.             NSLog(@"%@",fileInfo);    
  35.                   
  36.     }  
  37.       
  38.         //直接定位到要读取的文件  
  39.     if([unzipFile locateFileInZip:f]){  
  40.               
  41.             //取得读取文件流  
  42.         ZipReadStream *read2= [unzipFile readCurrentFileInZip];  
  43.         currentInfo = [unzipFile getCurrentFileInZipInfo];  
  44.           
  45.         //开始进行读取  
  46.         NSMutableData *data2= [[[NSMutableData alloc] initWithLength:currentInfo.length] autorelease];  
  47.         int bytesRead2= [read2 readDataWithBuffer:data2];  
  48.               
  49.         NSLog(@"SIZE IS:%d,Read size is :%d",currentInfo.length,bytesRead2);  
  50.               
  51.         NSString *fileText2= [[[NSString alloc] initWithBytes:[data2 bytes] length:bytesRead2 encoding:NSUTF8StringEncoding] autorelease];  
  52.         NSLog(@"%@",fileText2);  
  53.       
  54.         }  
  55.   
  56.           
  57.         [unzipFile release];  
  58.     } @catch (ZipException *ze) {  
  59.           
  60.         NSLog(@"ZipException caught: %d - %@", ze.error, [ze reason]);  
  61.           
  62.     } @catch (id e) {  
  63.               
  64.         NSLog(@"Exception caught: %@ - %@", [[e class] description], [e description]);  
  65.     }  
  66.       
  67.     [pool drain];  
  68.     return 0;  
  69. }  

你可能感兴趣的:(测试,iPhone,ide,encoding)