ZipArchive 解压zip

来源:互联网 发布:java jar 打包makefile 编辑:程序博客网 时间:2024/05/16 13:50
仿照网上的例子,自己学习了一下. 
1.将ZipArchive文件夹添加到项目里面. 
2.导入库(libz.dylib或者libz1.2.3.dylib,不知道区别,可能是版本号,反正都可以用) 
3.代码 
Java代码  收藏代码
  1. @interface TestViewViewController : UIViewController {  
  2.     UIImageView *imageView;  
  3.     UIButton *button;  
  4. }  
  5. @property (nonatomic,retain)UIImageView *imageView;  
  6. @property (nonatomic,retain)UIButton *button;  
  7. - (NSString *)dataFilePath:(NSString *)fileName;  
  8. @end  


Java代码  收藏代码
  1. #import "TestViewViewController.h"  
  2. #import "ZipArchive.h"  
  3.   
  4. @implementation TestViewViewController  
  5.   
  6. @synthesize imageView;  
  7. @synthesize button;  
  8.   
  9. - (void)loadView {  
  10.       
  11.     [super loadView];  
  12.       
  13.     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0110169255)];  
  14.     button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  15.     button.frame  = CGRectMake(0010050);  
  16.     [button addTarget:self action:@selector(myUnZip) forControlEvents:UIControlEventTouchDown];  
  17.       
  18.     [self.view addSubview:imageView];  
  19.     [self.view addSubview:button];  
  20. }  
  21. - (void)myUnZip {  
  22.       
  23.     //获取资源文件的路径  
  24.     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demozipfile.zip" ofType:nil];  
  25.     BOOL result;  
  26.     ZipArchive *zip = [[ZipArchive alloc] init];  
  27.     if ([zip UnzipOpenFile:filePath]) {  
  28.         result = [zip UnzipFileTo:[self dataFilePath:@"2011"] overWrite:YES];  
  29.         [zip UnzipCloseFile];  
  30.     }  
  31.     if (result) {  
  32.           
  33.         imageView.image = [UIImage imageWithContentsOfFile:[self dataFilePath:@"2011/nga_519887.png"]];  
  34.     }  
  35.     [zip release];  
  36. }  
  37.   
  38. -(NSString *)dataFilePath:(NSString *)fileName {  
  39.       
  40.     //获取文件的document文件夹的路径.  
  41.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  42.     NSString *documentDiretory = [paths objectAtIndex:0];  
  43.     //追加一个路径  
  44.     return [documentDiretory stringByAppendingPathComponent:fileName];  
  45. }  
  46. @end  



http://fightper.iteye.com/blog/1050203























原创粉丝点击