新建测试项目,以下代码可以正常运行,能够使用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