iOS_使用SSZipArchive解压文件

项目概述

在演示项目中,以下代码是可以正常运行,

能通过SSZipArchive 解压.zip的文件 (但是.rar.7z无法解压)

 


具体使用步骤

1.从github下载SSZipArchive,

然后连同vwhm.net.zip压缩文件一起, 拖动到项目里,

如图所示

使用SSZipArchive解压文件演示项目结构
使用SSZipArchive解压文件演示项目结构

编码

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:@"vwhm.net.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

然后Command + R 运行,完美解压vwhm.net.zip文件​~


移植时突发意外:

但是, 当把上面演示项目里的功能代码  移植  到 正式项目 帅哥日语 app中时,程序就会发生崩溃!

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

移植到其他项目时报错EXC_BAD_ACCESS
移植到其他项目时报错EXC_BAD_ACCESS

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

移植时又报错:pointer being freed was not allocated
移植时又报错:pointer being freed was not allocated

所以, 不得不放弃了

下篇介绍,帅哥日语app中实际应用的另外一个objective-zip压缩库

IOS_使用objective-zip解压文件


Github地址:

https://github.com/ixixii/iOS_unarchive