在项目过程中需要对从服务器拉取的压缩文件进行解压,并且需要把解压后的文件修改后再压缩上传到服务器;调研后使用ZipArchive对文件进行压缩和解压。
下面是ZipArchive的介绍和使用:
1、ZipArchive是压缩和解压在iOS和Mac文件的一种简单实用类,具有以下功能:
a.解压zip文件
b.解压加密的zip文件
c.创建zip文件
d.追加到创建的zip文件
e.压缩文件
f.通过NSData数据压缩
2、解压缩压缩后的文件:
-(BOOL)extractZipFile:(NSString *)filePath{
//filePath表示需要解压缩的压缩文件路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *savePath = [cachesPath stringByAppendingPathComponent:@"map"];//savePath表示将压缩文件解压到的路径 BOOL flag = [SSZipArchive unzipFileAtPath:filePath toDestination:savePath];//调用unzipFileAtPath解压缩文件,返回解压缩的结果
return flag;}
3、压缩某个文件路径下的文件:
-(BOOL)compressZipFile:(NSString *)filePath{
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachesPath stringByAppendingPathComponent:@"map"];
BOOL flag = [SSZipArchive createZipFileAtPath:filePath
withContentsOfDirectory:savePath];
return flag;
}
这里只是介绍了下项目中用到的最基本的压缩解压操作,ZipArchive还有很多其他的高级操作,详细看代码。
ZipArchive下载地址:
http://download.youkuaiyun.com/detail/guobing19871024/9684117