仿照网上的例子,自己学习了一下.
1.将ZipArchive文件夹添加到项目里面.
2.导入库(libz.dylib或者libz1.2.3.dylib,不知道区别,可能是版本号,反正都可以用)
3.代码
1.将ZipArchive文件夹添加到项目里面.
2.导入库(libz.dylib或者libz1.2.3.dylib,不知道区别,可能是版本号,反正都可以用)
3.代码
- @interface TestViewViewController : UIViewController {
- UIImageView *imageView;
- UIButton *button;
- }
- @property (nonatomic,retain)UIImageView *imageView;
- @property (nonatomic,retain)UIButton *button;
- - (NSString *)dataFilePath:(NSString *)fileName;
- @end
- #import "TestViewViewController.h"
- #import "ZipArchive.h"
- @implementation TestViewViewController
- @synthesize imageView;
- @synthesize button;
- - (void)loadView {
- [super loadView];
- imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 110, 169, 255)];
- button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button.frame = CGRectMake(0, 0, 100, 50);
- [button addTarget:self action:@selector(myUnZip) forControlEvents:UIControlEventTouchDown];
- [self.view addSubview:imageView];
- [self.view addSubview:button];
- }
- - (void)myUnZip {
- //获取资源文件的路径
- NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demozipfile.zip" ofType:nil];
- BOOL result;
- ZipArchive *zip = [[ZipArchive alloc] init];
- if ([zip UnzipOpenFile:filePath]) {
- result = [zip UnzipFileTo:[self dataFilePath:@"2011"] overWrite:YES];
- [zip UnzipCloseFile];
- }
- if (result) {
- imageView.image = [UIImage imageWithContentsOfFile:[self dataFilePath:@"2011/nga_519887.png"]];
- }
- [zip release];
- }
- -(NSString *)dataFilePath:(NSString *)fileName {
- //获取文件的document文件夹的路径.
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentDiretory = [paths objectAtIndex:0];
- //追加一个路径
- return [documentDiretory stringByAppendingPathComponent:fileName];
- }
- @end