事实上,在iOS开发中,压缩与解压,我都是采用第三方框架SSZipArchive实现的
gitHub地址: https://github.com/ZipArchive/ZipArchive
上面有详细的使用方法
因为ZipArchive不支持ARC,所以如果你的工程开启了ARC,那么就需要对ZipArchive设置一下。在ZipArchive.mm编译选项中,增加-fno-objc-arc即可。
最后,需要为工程链接libz.dylib动态链接库。
使用示范(压缩):
// 获得mainBundle中所有的png的图片路径
NSArray *pngs = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil]; // zip文件路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *zipFilepath = [caches stringByAppendingPathComponent:@"pngs.zip"]; // 创建zip文件
[SSZipArchive createZipFileAtPath:zipFilepath withFilesAtPaths:pngs];
解压:
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [caches stringByAppendingPathComponent:@"文件名.zip"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 解压(文件大, 会比较耗时,所以放到子线程中解压)
[SSZipArchive unzipFileAtPath:filepath toDestination:caches];
});