基于ZipArchive.zip
导入libz.dylib库
/**
* 根据路径将文件压缩为zip到指定路径
*
* @param sourcePath 压缩文件夹路径
* @param destZipFile存放路径(保存重命名)
*/
+(void) doZipAtPath:(NSString*)sourcePath to:(NSString*)destZipFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
ZipArchive * zipArchive = [ZipArchive new];
BOOL isdic;
//判断sourcePath下是文件夹还是文件
if(![fileManager fileExistsAtPath:sourcePath isDirectory:&isdic])
return;//文件已存在,直接返回
[zipArchive CreateZipFile2:destZipFile];
if (isdic)//文件夹
{
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:sourcePath error:nil];//文件列表
for(NSString *filePath in fileList){
NSString *fileName = [filePath lastPathComponent];//取得文件名
NSString *path = [sourcePath stringByAppendingString:[NSString stringWithFormat:@"/%@",filePath]];
[zipArchive addFileToZip:path newname:fileName];
}
}
else
{
[zipArchive addFileToZip:sourcePath newname:[sourcePath lastPathComponent]];
}
[zipArchive CloseZipFile2];
}