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