1. 导入三方包
包地址:
https://github.com/ZipArchive/ZipArchive
或者通过pod加载
pod 'SSZipArchive’
2. 使用
路径宏定义,方便使用
#define doc_path(_fileOrFolder_) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:_fileOrFolder_]
NSString *zippath = doc_path(@"zipfile.zip");//创建压缩文件路径
压缩,传入被压缩和压缩后文件的路径
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray<NSString *> *)paths
例如
[SSZipArchive createZipFileAtPath:zippath withFilesAtPaths:@[txtPath]];
解压缩,传入被解压和解压后文件路径
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination