iOS开发中压缩和解压文件

0.压缩+解压.gif

主要用到了一个三方:SSZipArchive
下载地址:https://github.com/Feijunjie/SSZipArchive

使用方法

压缩方法:

- (void)createZip{

NSString * path = [[NSBundle mainBundle] pathForResource:@"0.头像" ofType:@"png"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* zipPath = [documentPath stringByAppendingString:@"/images.zip"] ;
NSArray * pathArr = @[path];
// 对文件进行压缩
[SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:pathArr];
// 对文件夹进行压缩
//    [SSZipArchive createZipFileAtPath:zipPath withContentsOfDirectory:@"" keepParentDirectory:YES];
}

解压方法:

- (void)unzip{
NSString * path = [[NSBundle mainBundle] pathForResource:@"0.头像" ofType:@"zip"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* unZipTo = [documentPath stringByAppendingString:@"/images"] ;
[SSZipArchive unzipFileAtPath:path toDestination:unZipTo progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
    
} completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nonnull error) {
    NSLog(@"path:%@", path);
    NSLog(@"succeeded:%d", succeeded);
    NSLog(@"error:%@", error);
}];
}

demo地址:https://gitee.com/liangsenliangsen/compression_and_decompression.git

你可能感兴趣的:(iOS开发中压缩和解压文件)