iOS解压ZIP压缩包

下载地址:SSZipArchive

步骤:

一:导入SSZipArchive
使用cocoapods导入
pod 'SSZipArchive'

二:导入头文件
#import "SSZipArchive.h"

三:获取zip压缩包文件路径

  • 获取文件的方式
    方法一:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ZipName" ofType:@"zip"];

ZipName:zip文件名
zip:文件名后缀

如果压缩包是直接拖进工程的就使用这种方式获取文件路径。这是获取项目下的文件路径

方法二:Document目录下

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = paths.firstObject;
NSString *filePath = [basePath stringByAppendingPathComponent:@"文件名"];

方法三:Cache目录下

NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *basePath = paths.firstObject;
NSString *filePath = [basePath stringByAppendingPathComponent:@"文件名"]

四:解压

/**
 SSZipArchive解压

 @param path 压缩包文件路径
 */
-(void)uSSZipArchiveWithFilePath:(NSString *)path
{
    //Caches路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    //解压目标路径
    NSString *destinationPath =[cachesPath stringByAppendingPathComponent:@"SSZipArchive"];
   
    //解压
    BOOL isSuccess = [SSZipArchive unzipFileAtPath:path toDestination:destinationPath];
    
    //如果解压成功则获取解压后文件列表
    if (isSuccess) {
        [self obtainZipSubsetWithFilePath:destinationPath];
    }
    
}


/**
 获取解压后文件列表

 @param path 解压后的文件路径
 */
- (void)obtainZipSubsetWithFilePath:(NSString *)path
{
    NSString *destinationPath =[path stringByAppendingPathComponent:@"压缩包名(不需要后缀)"];
    // 读取文件夹内容
    NSError *error = nil;
    NSMutableArray*items = [[[NSFileManager defaultManager]
                             contentsOfDirectoryAtPath:destinationPath
                             error:&error] mutableCopy];
    if (error) {
        return;
    }
    
    for (NSString * item_str in items) {
        NSLog(@"文件名:%@",item_str);
    }
    
}

你可能感兴趣的:(iOS解压ZIP压缩包)