github地址 https://github.com/ZipArchive/ZipArchive
我的环境: xcode 8
1, 下载zip包, 解压, 拷贝里面的SSZipArchive目录到项目中
2, 删除SSZipArchive中的SSZipArchive+Swift.swift文件
3, targets ---> build Phases ---> linkBinary with libraries, 增加libz.tbd
4,需要的文件导入 #import "SSZipArchive.h"
遇到的问题,
1, 没有删除SSZipArchive+Swift.swift时, 会报错
“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured
correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…]
menu to choose a Swift version or use the Build Settings editor to configure the build
setting directly.
2, 我只用oc, 所以就删了那个swift的文件. 我也不知道怎么解决. 知道的同学, 可以告诉我一下.谢谢
------------- 压缩 ---------------------
// 获取需要压缩的文件数组
NSArray *inputPaths = [NSArray arrayWithObjects:[[NSBundle mainBundle]pathForResource:@"config.properties" ofType:nil], [[NSBundle mainBundle] pathForResource:@"b.mkv" ofType:nil], nil];
// 指定压缩到哪, 文件名叫什么
NSString *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *outputPath = [docpath stringByAppendingPathComponent:@"a.zip"];
NSLog(@"path:%@",outputPath);
// 返回的布尔值代表是否压缩完成
Boolean b = [SSZipArchive createZipFileAtPath:outputPath withFilesAtPaths:inputPaths];
------------ 简单解压 ------------
// 压缩包的全路径(包括文件名)
NSString *destinationPath = zipPath;
// 目标路径,
NSString *destinationPath = destinationPath;
// 解压, 返回值代表是否解压完成
Boolean b = [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath];
------------ 带回调的解压 ------------
Boolean b1 = [SSZipArchive unzipFileAtPath:zipPath toDestination:destinationPath progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
// entry : 解压出来的文件名
//entryNumber : 第几个, 从1开始
//total : 总共几个
NSLog(@"progressHandler:%@, , entryNumber:%ld, total:%ld", entry, entryNumber, total);
} completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nullable error) {
//path : 被解压的压缩吧全路径
//succeeded 是否成功
// error 错误信息
NSLog(@"completionHandler:%@, , succeeded:%d, error:%@", path, succeeded, error);
}];
网友评论