ZipArchive

使用这个框架需要导入库

#import "ViewController.h"
#import "SSZipArchive.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor redColor]];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self unzip];
}

- (void)zipFiles
{
    NSArray *array = @[@"/Users/zhangfan/Pictures/IMG_5993.PNG",
                       @"/Users/zhangfan/Pictures/IMG_5997.PNG"];

    //压缩一个数组中的所有文件
    [SSZipArchive createZipFileAtPath:@"/Users/zhangfan/Desktop/images.zip" withFilesAtPaths:array];
}

- (void)zipFilesWithPassword
{
    NSArray *array = @[@"/Users/zhangfan/Pictures/IMG_5993.PNG",
                       @"/Users/zhangfan/Pictures/IMG_5997.PNG"];

    //压缩文件后加一个解压密码
    [SSZipArchive createZipFileAtPath:@"/Users/zhangfan/Desktop/images.zip" withFilesAtPaths:array withPassword:@"123456"];
}

- (void)zipFolder
{
    //压缩一个文件夹内的所有内容
    [SSZipArchive createZipFileAtPath:@"/Users/zhangfan/Desktop/images.zip" withContentsOfDirectory:@"/Users/zhangfan/Desktop/images"];
}

- (void)unzip
{
    //解压哪个压缩包到哪个路径
    //[SSZipArchive unzipFileAtPath:@"/Users/zhangfan/Desktop/images.zip" toDestination:@"/Users/zhangfan/Desktop/"];

    //解压压缩包至某路径,并且可以监测解压的实时情况
    [SSZipArchive unzipFileAtPath:@"/Users/zhangfan/Desktop/images.zip" toDestination:@"/Users/zhangfan/Desktop/images" progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
        NSLog(@"正在解压第:%ld 个文件,总共 %ld个文件",entryNumber,total);
    } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
        NSLog(@"解压文件到路径:%@,是否成功:%d",path,succeeded);
    }];

    /*
     2016-12-14 14:46:12.832 zipTest[2170:67758] 正在解压第:1 个文件,总共 2个文件
     2016-12-14 14:46:12.987 zipTest[2170:67758] 正在解压第:2 个文件,总共 2个文件
     2016-12-14 14:46:12.988 zipTest[2170:67758] 解压文件到路径:/Users/zhangfan/Desktop/images.zip,是否成功:1
     */
}

@end

results matching ""

    No results matching ""