添加Ziparchive框架 pod 'ZipArchive', '~> 1.3.0'
link binrary with Libraies添加库的framework文件
引入头文件 #import <ZipArchive/ZipArchive.h>
/**
* 根据路径将文件压缩为zip到指定路径
*
* @param sourcePath 压缩文件夹路径
* @param destZipFile存放路径(保存重命名)路径.zip结尾
*/
-(void) doZipAtPath:(NSString*)sourcePath to:(NSString*)destZipFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
ZipArchive * zipArchive = [ZipArchive new];
[zipArchive CreateZipFile2:destZipFile];
NSArray *subPaths = [fileManager subpathsAtPath:sourcePath];// 关键是subpathsAtPath方法
for(NSString *subPath in subPaths){
NSString *fullPath = [sourcePath stringByAppendingPathComponent:subPath];
BOOL isDir;
if([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && !isDir)// 只处理文件
{
[zipArchive addFileToZip:fullPath newname:subPath];
}
}
[zipArchive CloseZipFile2];
}
转自:https://blog.csdn.net/jinsulei123/article/details/26086839