iOS_48_使用SSZipArchive解压文件

新建测试项目,以下代码可以正常运行,能够使用SSZipArchive正常解压后缀为.zip的文件 (.rar和.7z的解压不能)

1.从github下载SSZipArchive,然后连同abc.zip压缩文件一起,拖动到项目里,如图所示



2.将以下代码一个字一个敲到控制器里即可

//
//  ViewController.m
//  tmpZipperIos
//
//  Created by beyond on 2018/1/28.
//  Copyright © 2018年 beyond. All rights reserved.
//

#import "ViewController.h"
#import "SSZipArchive.h"
@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //    [self startZip];
    
    [self startUnZip];
    
    
    
}
#pragma mark - 压缩
- (void)startZip
{
    NSString *sampleDataPath = [[NSBundle mainBundle].bundleURL
                                URLByAppendingPathComponent:@"Sample Data"
                                isDirectory:YES].path;
    
    NSLog(@"sg__sampleDataPath:%@",sampleDataPath);
    NSString *zipPath = [self tempZipPath];
    NSLog(@"sg___zipPath:%@",zipPath);
    
    NSString *password = @"";
    BOOL success = [SSZipArchive createZipFileAtPath:zipPath
                             withContentsOfDirectory:sampleDataPath
                                 keepParentDirectory:NO
                                    compressionLevel:-1
                                            password:password.length > 0 ? password : nil
                                                 AES:YES
                                     progressHandler:nil];
    if (success) {
        NSLog(@"Success zip");
        
        
//        [self startUnZip];
    } else {
        NSLog(@"No success zip");
    }
}


- (NSString *)tempZipPath {
    NSString *path = [NSString stringWithFormat:@"%@/\%@.zip",
                      NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
                      [NSUUID UUID].UUIDString];
    return path;
}

#pragma mark - 解压
- (void)startUnZip
{
    NSString *unzipPath = [self tempUnzipPathInDoc];
    
    
    
    
    NSString *zipPath = [[NSBundle mainBundle] pathForResource:@"abc.zip" ofType:nil];
    NSLog(@"sg__zip:%@",zipPath);
    
    NSLog(@"sg__unzip:%@",unzipPath);
    BOOL success = [SSZipArchive unzipFileAtPath:zipPath
                                   toDestination:unzipPath
                              preserveAttributes:YES
                                       overwrite:YES
                                  nestedZipLevel:0
                                        password:nil
                                           error:nil
                                        delegate:nil
                                 progressHandler:nil
                               completionHandler:nil];
    if (success) {
        NSLog(@"Success unzip");
    } else {
        NSLog(@"No success unzip");
        return;
    }
    
    
}

- (NSString *)tempUnzipPath {
    NSString *path = [NSString stringWithFormat:@"%@/\%@",
                      NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0],
                      [NSUUID UUID].UUIDString];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    [[NSFileManager defaultManager] createDirectoryAtURL:url
                             withIntermediateDirectories:YES
                                              attributes:nil
                                                   error:&error];
    if (error) {
        return nil;
    }
    return url.path;
}

- (NSString *)tempUnzipPathInDoc
{
    NSString *path = [NSString stringWithFormat:@"%@/\%@",
                      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0],
                      [NSUUID UUID].UUIDString];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    [[NSFileManager defaultManager] createDirectoryAtURL:url
                             withIntermediateDirectories:YES
                                              attributes:nil
                                                   error:&error];
    if (error) {
        return nil;
    }
    return url.path;
}

@end


但是移植到其他项目中,就会崩溃!

错误提示如下: EXC_BAD_ACCESS(code=1,address=0x0)


malloc: *** mach_vm_map(size=1881309184) failed (error code = 3)

*** error: can't allocate region

*** set a breakpoint in malloc_error_break to debug


有时候又会报这样的错误:

malloc: *** error for object 0x1700d5930: pointer being freed was not allocated

*** set a breakpoint in malloc_error_break to debug






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值