压缩与解压缩

1.需要使用第三方库SSZipArchive,需要增加libz库

1.压缩文件   
 //1.要压缩的文件
    NSArray *images = @[@"/Users/xingzai/Desktop/Snip20170424_7.png",@"/Users/xingzai/Desktop/Snip20170424_1.png"];
    //2.开始压缩
    /**
     1.createZipFileAtPath ,压缩文件到位置
     2.withFilesAtPaths ,压缩文件的路径
     */
// 不需要用密码的
 //  [SSZipArchive createZipFileAtPath:@"/Users/xingzai/Desktop/1.zip" withFilesAtPaths:images];
//需要用密码的
    [SSZipArchive createZipFileAtPath:@"/Users/xingzai/Desktop/1.zip" withFilesAtPaths:images withPassword:@"123456"];
2.压缩文件夹
/*
     第一个参数:压缩文件存放位置
     第二个参数:要压缩的文件夹(目录)
     */
    [SSZipArchive createZipFileAtPath:@"/Users/xingzai/Desktop/Tools.zip" withContentsOfDirectory:@"/Users/xingzai/Desktop/Tools"];
3.解压文件或文件夹
 
    /*
     第一个参数:要解压的文件在哪里
     第二个参数:文件应该解压到什么地方
     */

    //解压1
[SSZipArchive unzipFileAtPath:@"/Users/xiaomage/Desktop/demo.zip" toDestination:@"/Users/xiaomage/Desktop/xx"];
    //解压2
    [SSZipArchive unzipFileAtPath:@"/Users/xingzai/Desktop/1.zip" toDestination:@"/Users/xingzai/Desktop/xx" overwrite:YES password:@"123456" progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
        
    } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
        
    }];
//解压3
    [SSZipArchive unzipFileAtPath:@"/Users/xingzai/Desktop/Tools.zip" toDestination:@"/Users/xingzai/Desktop/xx" progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
        NSLog(@"%zd---%zd",entryNumber,total);
        
    } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {

        NSLog(@"%@",path);
    }];

  

原文地址:https://www.cnblogs.com/TheYouth/p/6764945.html