ZipArchive 解压zip

仿照网上的例子,自己学习了一下. 
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  


























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值