使用SSZipArchive第三方库解压zip包

本文介绍了一种在iOS应用中实现zip文件压缩与解压缩的方法。通过使用SSZipArchive库,可以轻松地将多个PNG图片文件打包成zip格式,并支持设置密码保护。此外,还演示了如何将压缩文件解压到缓存目录。
//
//  ViewController.m
//  解压缩zip包开发
//
//  Created by 张凯泽 on 16/3/9.
//  Copyright © 2016年 rytong_zkz. All rights reserved.
//

#import "ViewController.h"
#import "SSZipArchive.h"
@interface ViewController ()
- (IBAction)PressZipClick:(id)sender;
- (IBAction)UnarchiveZipClick:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

//压缩zip包
- (IBAction)PressZipClick:(id)sender {
    NSArray *pngs = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil];
    
    // zip文件路径
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *zipFilepath = [caches stringByAppendingPathComponent:@"pngs.zip"];
    NSLog(@"zipFilepath = %@",zipFilepath);
    
    // 创建zip文件
    //[SSZipArchive createZipFileAtPath:zipFilepath withFilesAtPaths:pngs];
    [SSZipArchive createZipFileAtPath:zipFilepath withFilesAtPaths:pngs withPassword:@"zkz"];
}
 //解zip包
- (IBAction)UnarchiveZipClick:(id)sender {
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath = [caches stringByAppendingPathComponent:@"pngs.zip"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 解压(文件大, 会比较耗时,所以放到子线程中解压)
        //[SSZipArchive unzipFileAtPath:filepath toDestination:caches];
        NSError * error;
        [SSZipArchive unzipFileAtPath:filepath toDestination:caches overwrite:YES password:@"zkz" error:&error];
        NSLog(@"error = %@",error);
        NSLog(@"解压完成");
    });
}
@end

 

转载于:https://www.cnblogs.com/zkzzkz/p/5258365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值