iOS ZipArchive
来源:互联网 发布:淘宝修身包臀连衣裙 编辑:程序博客网 时间:2024/06/05 18:02
一、为什么我需要解压缩文件?
1、苹果App Store的50M下载限制
苹果公司出于流量的考虑,规定在非WIFI环境下,限制用户只能下载小于50M的应用或游戏。这样一来,对于一些数据或数据包较大的应用,我们只能尽量减小 应用二进制包的体积。而把数据打包到zip中,这样App可以通过网络下载数据包,解压出所需要的内容,而且这样也可以动态的更新内容。
2、动态更新内容
这一点在上面已经提过了。如果应用所需要的资源需要动态更新,一种常见的做法是更新资源,重新打包,重新提交到App store,这样做你需要等待漫长的审核、上架时间。一般情况下是一周左右的时间。更好的方法是将这些资源打包放置在服务器上,App从服务器(或者云存 储)上下载,然后解压。这样做的好处显而易见,那就是可以快速更新,动态更新,不需要重新打包、上传、审核,省时省力
3、从Web上下载zip文件
Safari和邮件程序都不支持zip的查看,通过ZipArchive你就可以为你的设备增加查看zip文件的能力了,尽管App Store里已经有一些App支持这些功能了。
二、代码实现
1、首先,下载从github上下载ZipArchive开源代码,地址如下 https://github.com/ZipArchive/ZipArchive
2、将其添加到项目中,并在链接库中添加 libz.tbd 和用于压缩测试的文件夹
3、实现代码如下
//// ViewController.m// ZipArchiveDemo//// Created by 555chy on 6/23/16.// Copyright © 2016 555chy. All rights reserved.//#import "ViewController.h"#import "SSZipArchive/SSZipArchive.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //获取程序临时文件夹目录 NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"dirPath is %@", dirPath); BOOL result; //压缩 NSString *zipPath = [dirPath stringByAppendingPathComponent:@"testZip.zip"]; NSLog(@"zipPath is %@", zipPath); NSString *path11 = [[NSBundle mainBundle] pathForResource:@"test11" ofType:@"txt"]; NSString *path12 = [[NSBundle mainBundle] pathForResource:@"test12" ofType:@"txt"]; NSString *path21 = [[NSBundle mainBundle] pathForResource:@"TestResource2/test21" ofType:@"txt"]; NSString *path22 = [[NSBundle mainBundle] pathForResource:@"TestResource2/TestFolder/test22" ofType:@"txt"]; NSString *folder1 = [[NSBundle mainBundle] pathForResource:@"TestResource1" ofType:nil]; NSString *folder2 = [[NSBundle mainBundle] pathForResource:@"TestResource2" ofType:nil]; NSLog(@"path11 = %@", path11); NSLog(@"path12 = %@", path12); NSLog(@"path21 = %@", path21); NSLog(@"path22 = %@", path22); NSLog(@"folder1 = %@", folder1); NSLog(@"folder2 = %@", folder2); NSArray *inputFileArray = [NSArray arrayWithObjects: path11, path12, path21, path22, nil]; //该方法只能压缩入文件,不能存入文件夹 //result = [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:inputFileArray]; //该方法只能压缩目录,keepParentDirectory表示是否将最外层的文件夹也导入进去 result = [SSZipArchive createZipFileAtPath:zipPath withContentsOfDirectory:folder2 keepParentDirectory:YES]; NSLog(@"zip file %@", result?@"success":@"fail"); //解压 NSString *unzipPath = [dirPath stringByAppendingPathComponent:@"testUnzip/"]; NSFileManager *fileManager = [NSFileManager defaultManager]; //删除待解压目录及其所有子文件 [fileManager removeItemAtPath:unzipPath error:nil]; NSLog(@"unzipPath is %@", unzipPath); result = [SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:nil]; NSLog(@"unzip file %@", result?@"success":@"fail"); //判断相应文件或文件夹是否存在 BOOL isDir; result = [fileManager fileExistsAtPath:zipPath isDirectory:&isDir]; NSLog(@"zip file is %@ and it's %@", result?@"exits":@"not exits", isDir?@"directory":@"file"); result = [fileManager fileExistsAtPath:unzipPath isDirectory:&isDir]; NSLog(@"unzip file is %@ and it's %@", result?@"exits":@"not exits", isDir?@"directory":@"file"); //枚举路径下的文件 NSLog(@"枚举方法1"); //enumeratorAtPath递归获取目录下的所有文件(遇到文件夹则进入获取子文件) NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:unzipPath]; int i=0; NSString *path; while (path = [dirEnum nextObject]) { NSLog(@"unzip file %d = %@", i, path); i++; } NSLog(@"枚举方法2"); //contentsOfDirectoryAtPath只能获取当前目录下的文件(或文件夹),不递归 NSArray<NSString *> *subfileArray = [fileManager contentsOfDirectoryAtPath:unzipPath error:nil]; NSLog(@"unzip file count = %ld", [subfileArray count]); i=0; for(path in subfileArray) { NSLog(@"unzip file %d = %@", i, path); i++; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
4、运行截图
0 0
- IOS ZipArchive
- iOS ZipArchive
- iOS 使用ZipArchive压缩文件
- iOS 使用ZipArchive压缩文件
- iOS 使用ZipArchive压缩文件
- ios压缩文件夹--基于ZipArchive
- IOS 使用ZipArchive解压文件
- ZipArchive
- ZipArchive
- ZipArchive
- IOS下利用ziparchive解压缩zip文件
- IOS下利用ziparchive解压缩zip文件
- IOS下利用ziparchive解压缩zip文件
- iOS中使用ZipArchive 压缩和解压缩文件
- IOS下利用ziparchive解压缩zip文件
- ios用ZipArchive组件进行压缩
- iOS用ZipArchive解压zip包
- 在iOS使用ZipArchive压缩和解压缩文件
- 用Beyond Compare和输出文件怎么进行比较
- caffe学习
- FILE* fp
- arm-linux-gnueabi-gcc编译uboot出错
- [HTML]显示/隐藏DIV的技巧(visibility与display的差别)
- iOS ZipArchive
- c++构造函数以及类中变量初始化顺序
- sysfs API总结
- 【创客笔记】:慎用BAT的人
- niagara编程中需要注意的几点问题
- 建造者模式
- 云端观察:传统教育如何应对三大挑战?
- 安卓中sharepreference总结
- Ryan的OC学习总结-----6 块