1、简介
- SSZipArchive是iOS和Mac上一个简单实用的压缩和解压插件。用途包括:
- 1.解压zip文件;
- 2.解压密码保护的ZIP文件;
- 3.创建新的zip文件;
- 4.追加文件到现有的压缩;
- 5.压缩文件;
- 6.压缩NSData(带有文件名)
- SSZipArchive的GitHub地址:https://github.com/ZipArchive/ZipArchive
2、压缩方法
- 压缩指定文件代码:
// SSZipArchive压缩
- (void)ssZipArchiveWithFiles {
// Caches路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
// zip压缩包保存路径
NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
// 需要压缩的文件
NSArray *filesPath = @[
@"/Users/apple/Desktop/demo/LaunchImage-2-700-568h@2x.png",
@"/Users/apple/Desktop/demo/LaunchImage-2-700@2x.png",
@"/Users/apple/Desktop/demo/LaunchImage-2-800-667h@2x.png",
@"/Users/apple/Desktop/demo/LaunchImage-2-800-Landscape-736h@3x.png"
];
// 创建不带密码zip压缩包
BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath];
// 创建带密码zip压缩包
//BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withFilesAtPaths:filesPath withPassword:@"SSZipArchive.zip"];
}
- 压缩指定文件夹代码:
// SSZipArchive压缩
- (void)ssZipArchiveWithFolder {
// Caches路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
// zip压缩包保存路径
NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
// 需要压缩的文件夹路径
NSString *folderPath = @"/Users/apple/Desktop/demo/";
// 创建不带密码zip压缩包
BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath ];
// 创建带密码zip压缩包
//BOOL isSuccess = [SSZipArchive createZipFileAtPath:path withContentsOfDirectory:folderPath withPassword:@"SSZipArchive.zip"];
}
3、解压方法
代码:
// SSZipArchive解压
-(void)uSSZipArchive {
// Caches路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
// 解压目标路径
NSString *destinationPath = [cachesPath stringByAppendingPathComponent:@"SSZipArchive"];
// zip压缩包的路径
NSString *path = [cachesPath stringByAppendingPathComponent:@"SSZipArchive.zip"];
// 解压
BOOL isSuccess = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath];
// 带密码的解压
// 出错原因
NSError *error;
// 解压
BOOL unzip_ok = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath
overwrite:YES password:@"123456" error:&error];
}