1、ZipArchive 方式

  • ZipArchive 只能对 zip 类文件进行压缩和解压缩

  • GitHub 网址:https://github.com/ZipArchive/ZipArchive

  • ZipArchive 使用 ARC

  • 添加 ZipArchive

    	// 添加第三方库文件
    ZipArchive // 包含系统动态库
    libz.tbd (libz.dylib) // 包含头文件
    #import "ZipArchive.h"
  • ZipArchive 压缩

    • 文件压缩

      	// 目标路径
      NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 源路径,待压缩的文件
      NSArray *resourcesFilePaths = @[@"/Users/JHQ0228/Desktop/test1.rtf",
      @"/Users/JHQ0228/Desktop/test2.rtf"]; BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
      withFilesAtPaths:resourcesFilePaths];
    • 文件夹压缩

      	// 目标路径
      NSString *destinationFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 源路径,待压缩的文件夹
      NSString *resourcesDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive createZipFileAtPath:destinationFilePath
      withContentsOfDirectory:resourcesDirPath];
  • ZipArchive 解压缩

    • 普通解压

      	// 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath];
    • 密码解压

      	// 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      overwrite:YES
      password:@"password"
      error:nil];
    • 协议解压

      	// 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      delegate:self];
    • 协议密码解压

      	// 源路径,待解压缩的文件
      NSString *resourcesFilePath = @"/Users/JHQ0228/Desktop/test.zip"; // 目标路径
      NSString *destinationDirPath = @"/Users/JHQ0228/Desktop/test"; BOOL result = [SSZipArchive unzipFileAtPath:resourcesFilePath
      toDestination:destinationDirPath
      overwrite:YES
      password:@"password"
      error:NULL
      delegate:self];
    • 协议方法

      	- (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total {
      
      		NSLog(@"%f", (float)loaded/total);
      }

2、SARUnArchiveANY 方式

  • SARUnArchiveANY 可以对 .zip, .rar, .7z 类文件进行压缩和解压缩。

  • GitHub 网址:https://github.com/saru2020/SARUnArchiveANY

  • SARUnArchiveANY 使用 ARC

  • 添加 SARUnArchiveANY

    	// 添加第三方库文件
    SARUnArchiveANY // 包含系统动态库
    libz.tbd (libz.dylib) // 包含头文件
    #import "SARUnArchiveANY.h"
    #import "LZMAExtractor.h"
  • SARUnArchiveANY 解压缩

    	- (void)unZip {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example" ofType:@"zip"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:nil destinationPath:destPath];
    } - (void)unRar {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"rar"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:nil destinationPath:destPath];
    } - (void)unZip_pwd {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Zip_Example_pwd" ofType:@"zip"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:@"SARUnArchiveANY_ZIP" destinationPath:destPath];
    } - (void)unRar_pwd {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example_pwd" ofType:@"rar"];
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:filePath andPassword:@"SARUnArchiveANY_RAR" destinationPath:destPath];
    } - (void)Unzip7z {
    NSString *archiveFilename = @"example.7z";
    NSString *archiveResPath = [[NSBundle mainBundle] pathForResource:archiveFilename ofType:nil];
    NSAssert(archiveResPath, @"can't find .7z file");
    NSString *destPath = [self applicationDocumentsDirectory];
    [self unArchive:archiveResPath andPassword:nil destinationPath:destPath];
    } - (void)unArchive: (NSString *)filePath andPassword:(NSString*)password destinationPath:(NSString *)destPath { NSAssert(filePath, @"can't find filePath"); SARUnArchiveANY *unarchive = [[SARUnArchiveANY alloc] initWithPath:filePath]; if (password != nil && password.length > 0) {
    unarchive.password = password;
    } if (destPath != nil) { // (Optional). If it is not given, then the file is unarchived in the same location of its archive/file.
    unarchive.destinationPath = destPath;
    } unarchive.completionBlock = ^(NSArray *filePaths) { NSLog(@"For Archive : %@", filePath); if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"US Presidents://"]]) { NSLog(@"US Presidents app is installed.");
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"US Presidents://"]];
    } for (NSString *filename in filePaths) {
    NSLog(@"File: %@", filename);
    }
    }; unarchive.failureBlock = ^(){
    NSLog(@"Cannot be unarchived");
    }; [unarchive decompress];
    } - (void)handleFileFromURL:(NSString *)filePath {
    NSLog(@"********* FILES FROM THE OTHER APPS *********");
    [self unArchive:filePath andPassword:nil destinationPath:nil];
    } - (NSString *) applicationDocumentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
    }

iOS - File Archive/UnArchive 文件压缩/解压的更多相关文章

  1. Linux 之 文件压缩解压

    文件压缩解压 参考教程:[千峰教育] 命令: gzip: 作用:压缩文件,只能是单个文件,不能是多个,也不能是目录. 格式:gzip file 说明:执行命令会生成file.gz,删除原来的file ...

  2. linux驱动系列之文件压缩解压小节(转)

    转至网页:http://www.jb51.net/LINUXjishu/43356.html Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通 ...

  3. 分享一个ASP.NET 文件压缩解压类 C#

    需要引用一个ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; ...

  4. linux文件压缩解压命令

    01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...

  5. Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接

    作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...

  6. linux命令:压缩解压命令

    压缩解压命令:gzip 命令名称:gzip 命令英文原意:GNU zip 命令所在路径:/bin/gzip 执行权限:所有用户 语法:gzip 选项  [文件] 功能描述:压缩文件 压缩后文件格式:g ...

  7. c#自带压缩类实现的多文件压缩和解压

    用c#自带的System.IO.Compression命名空间下的压缩类实现的多文件压缩和解压功能,缺点是多文件压缩包的解压只能调用自身的解压方法,和现有的压缩软件不兼容.下面的代码没有把多文件的目录 ...

  8. 对称加密之AES、压缩解压以及压缩加密解密解压综合实战

    AES 压缩解压 压缩加密解密解压 对称加密: 就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密.密钥是控制加密及解密过程的指令.算法是一组规则,规定如何进行加密和解密.   因此加密的安 ...

  9. linux笔记:linux常用命令-压缩解压命令

    压缩解压命令:gzip(压缩文件,不保留原文件.这个命令不能压缩目录) 压缩解压命令:gunzip(解压.gz的压缩文件) 压缩解压命令:tar(打包压缩目录或者解压压缩文件.打包的意思是把目录打包成 ...

随机推荐

  1. jenkins+jmeter+ant搭建接口测试平台

    接口测试的重点是检查数据的交换,传递和控制管理过程以及系统间的相互逻辑依赖关系. 接口测试的流程 项目启动后,测试人员要尽早拿到接口测试文档. 开始编写接口测试用例 将接口测试用例部署到持续集成的测试 ...

  2. href="#"会导致location.replace(location.href);脚本不工作

    我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...

  3. iOS 开发 证书总结 开发证书和生产证书的区别

    IOS开发 证书总结 开发者证书   ------>>  开发证书是你在真机推送时 用得, 生产证书是app 上架之后 推送给用户用的 首先你必须获得apple开发者证书,上图这个文件就是 ...

  4. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  5. 八大排序算法之五--交换排序—冒泡排序(Bubble Sort)

    基本思想: 在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将 ...

  6. 减少C++代码编译时间的方法

    c++ 的代码包含头文件和实现文件两部分, 头文件一般是提供给别人(也叫客户)使用的, 但是一旦头文件发生改变,不管多小的变化,所有引用他的文件就必须重新编译,编译就要花时间,假如你做的工程比较大(比 ...

  7. LA 4126 Password Suspects

    问题描述:给定m个模式串,计数包含所有模式串且长度为n的字符串的数目. 数据范围:模式串长度不超过10,m <= 10, n <= 25,此外保证答案不超过1015. 分析:既然要计数给定 ...

  8. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  9. Round Numbers(组合数学)

    Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...

  10. 深入Linux网络核心堆栈(对于netfilter的用法和讲解)

    http://blog.csdn.net/wswifth/article/details/5115475 注册一个hook函数是围绕nf_hook_ops数据结构的一个非常简单的操作,nf_hook_ ...