宽容他人,放过自己。

文件压缩解压缩

Posted on By anchoriteFili

参考资料


使用方法


1. 创建项目,下载SSZipArchive.zip,将SSZipArchive文件夹导入到项目中

752372-20161019194439232-1230713654.png

2. 导入三方需要的libz.tbd,否则会报错

752372-20161019194604138-34964487.png

导入头文件 #import “SSZipArchive.h” 即可进行相关操作
#import "ViewController.h"
#import "SSZipArchive.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}

#pragma mark 压缩点击事件
- (IBAction)compressionClick:(UIButton *)sender {
    
    /** 压缩文件夹
     zipFile :产生的zip文件的最终路径
     directory : 需要进行的压缩的文件夹路径
     */
    
    NSString *zipFile = @"/Users/macbook/Desktop/压缩测试/zip压缩文件.zip";
    NSString *directory = @"/Users/macbook/Desktop/压缩测试/zip压缩文件";
    
    [SSZipArchive createZipFileAtPath:zipFile withContentsOfDirectory:directory];
    
    
    /** 文件的压缩
     zipFile :产生的zip文件的最终路径
     files : 这是一个数组,数组里面存放的是需要压缩的文件的路径
     files = @[@”/Users/apple/Destop/1.png”, @”/Users/apple/Destop/3.txt”]
     */
    NSString *zipFileOne = @"/Users/macbook/Desktop/压缩测试/img.zip";
    NSArray *files = @[@"/Users/macbook/Desktop/压缩测试/zip压缩文件/Snip20161019_9.png",@"/Users/macbook/Desktop/压缩测试/zip压缩文件/Snip20161019_10.png"];
    
    [SSZipArchive createZipFileAtPath:zipFileOne withFilesAtPaths:files];
    
}

#pragma mark 解压缩
- (IBAction)decompressionClick:(UIButton *)sender {
    
    NSURL *url = [NSURL URLWithString:@"http://www.gonghuizhudi.com/file/images.zip"];
    
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        
        /** 解压缩
         location.path : 貌似是申请下来的数据的路径
         caches :解压缩后的存储路径
         */
        
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"caches ======= %@",caches);
        NSLog(@"location.path ====== %@",location.path);
        [SSZipArchive unzipFileAtPath:location.path toDestination:caches];
    }];
    [task resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end