https://blog.csdn.net/zhengang007/article/details/51019479

2016年03月30日  版权声明:本文为博主原创文章,转载请注明作者和原文链接。 https://blog.csdn.net/zhengang007/article/details/51019479

一、使用详解:

我们在开发app的时候,有时会需要对文件进行压缩和解压的操作,这个时候我们就必须要用到一个第三方的开源库,SSZipArchive ,来对目标文件进行压缩和解压的操作。
SSZipArchive下载链接,引入到工程时需要添加 libz.tbd 库,否则编译时通不过。
压缩:

//压缩
- (void)createZipFile {
//目的路径
NSString *destinationPath = @"/Users/Administrator/Desktop/wzg.zip";//注意是这个是 zip 格式的后缀
//源文件路径
NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.txt";
//数组里可以放多个源文件,这些文件会被同一打包成压缩包,到 destinationPath 这个路径下。
if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:@[sourceFilePath]]) {
NSLog(@"压缩成功");
}
else {
NSLog(@"压缩失败");
}
}
解压:

//解压
- (void)unzipFile {
//源文件路径
NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.zip";
//目的文件路径
NSString *destinationPath = @"/Users/wangzhengang/Desktop/";
//把 sourceFilePath 这个路径下的zip包,解压到这个 destinationPath 路径下
if ([SSZipArchive unzipFileAtPath:sourceFilePath toDestination:destinationPath delegate:self uniqueId:nil]){
NSLog(@"解压成功");
}
else {
NSLog(@"解压失败");
}
}

代理方法:

#pragma mark - SSZipArchiveDelegate
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo {
NSLog(@"将要解压。");
}

- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId {
NSLog(@"解压完成!");
}

二、SSZipArchive所有方法说明:

@interface SSZipArchive : NSObject

// Unzip 解压
/**
* @param path 源文件
* @param destination 目的文件
* @param uniqueId 标记,用于区别多个解压操作
*
* @return 返回 YES 表示成功,返回 NO 表示解压失败。
*/
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination uniqueId:(NSString *)uniqueId;

/**
* @param path 源文件
* @param destination 目的文件
* @param overwrite YES 会覆盖 destination 路径下的同名文件,NO 则不会。
* @param password 需要输入密码的才能解压的压缩包
* @param error 返回解压时遇到的错误信息
* @param uniqueId 标记,用于区别多个解压操作
*
* @return 返回 YES 表示成功,返回 NO 表示解压失败。
*/
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error uniqueId:(NSString *)uniqueId;

/**
* @param path 源文件
* @param destination 目的文件
* @param delegate 设置代理
* @param uniqueId 标记,用于区别多个解压操作
*
* @return 返回 YES 表示成功,返回 NO 表示解压失败。
*/
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;

/**
* @param path 源文件
* @param destination 目的文件
* @param overwrite YES 会覆盖 destination 路径下的同名文件,NO 则不会。
* @param password 需要输入密码的才能解压的压缩包
* @param error 返回解压时遇到的错误信息
* @param delegate 设置代理
* @param uniqueId 标记,用于区别多个解压操作
*
* @return 返回 YES 表示成功,返回 NO 表示解压失败。
*/
+ (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error delegate:(id<SSZipArchiveDelegate>)delegate uniqueId:(NSString *)uniqueId;

// Zip 压缩
/**
* @param path 目的路径(格式:~/xxx.zip 结尾的路径)
* @param filenames 要压缩的文件路径
*
* @return 返回 YES 表示成功,返回 NO 表示压缩失败。
*/
+ (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;

/**
* @param path 目的路径(格式:~/xxx.zip 结尾的路径)
* @param filenames 要压缩的文件目录路径
*
* @return 返回 YES 表示成功,返回 NO 表示压缩失败。
*/
+ (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;

/**
* 初始化压缩对象
*
* @param path 目的路径(格式:~/xxx.zip 结尾的路径)
*
* @return 初始化后的对像
*/
- (id)initWithPath:(NSString *)path;

/**
* 打开压缩对象
* @return 返回 YES 表示成功,返回 NO 表示失败。
*/
- (BOOL)open;

/**
* 添加要压缩的文件的路径
*
* @param path 文件路径
*
* @return 返回 YES 表示成功,返回 NO 表示失败。
*/
- (BOOL)writeFile:(NSString *)path;

/**
* 向此路径的文件里写入数据
*
* @param data 要写入的数据
* @param filename 文件路径
*
* @return 返回 YES 表示成功,返回 NO 表示失败。
*/
- (BOOL)writeData:(NSData *)data filename:(NSString *)filename;

/**
* 关闭压缩对象
* @return 返回 YES 表示成功,返回 NO 表示失败。
*/
- (BOOL)close;

@end

@protocol SSZipArchiveDelegate <NSObject>

@optional

//将要解压
- (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
//解压完成
- (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPat uniqueId:(NSString *)uniqueId;
//将要解压
- (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
//解压完成
- (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;

@end

三、遇到的问题:

当对要压缩或者要解压的文件的文件名包含有中文文字时,这个时候会出现文件名乱码的问题,或者在目的路径下未能找到解压后的文件的问题。
解决办法:

在 SSZipArchive.m 文件中改一下对 文件路径的编码格式,即可。
更改前:

NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];

更改后:

NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

NSString *strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];

---------------------
作者:飞天舞桐
来源:CSDN
原文:https://blog.csdn.net/zhengang007/article/details/51019479
版权声明:本文为博主原创文章,转载请附上博文链接!

iOS SSZipArchive--压缩与解压缩

https://www.jianshu.com/p/d80fb939d4c4

2018.01.09 16:15* 字数 62 阅读 780评论 2喜欢 1

配置SSZipArchive

导入SSZipArchive后,先编译,会出现如下错误:

 
异常.png

解决方法:
单击项目->Linked Frameworks and Libraries->点击左下角加号->输入libz.tbd->Add,再次编译即可。

 
图示.png

解压缩

NSString *str = @"/Users/mazaiting/Desktop/tomcat.zip";

[SSZipArchive unzipFileAtPath:str toDestination:@"/Users/mazaiting/Desktop/tomcat"];

压缩

[SSZipArchive createZipFileAtPath:@"/Users/mazaiting/Desktop/tomcat1.zip" withContentsOfDirectory:@"/Users/mazaiting/Desktop/tomcat"];

作者:_凌浩雨

链接:https://www.jianshu.com/p/d80fb939d4c4

来源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

SSZipArchive的使用详解和遇到的问题的更多相关文章

  1. Podfile文件用法详解

    https://www.jianshu.com/p/b8b889610b7e 2018.01.09 15:51* 字数 2343 阅读 6263评论 3喜欢 34 前言 iOS开发会经常用到cocoa ...

  2. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  3. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  4. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  5. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  6. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  7. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  8. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  9. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

随机推荐

  1. nginx报错 [error] open() “/usr/local/var/run/openresty.pid” failed (2: No such file or directory)

    解决: 服务没有启动 使用start启动服务,因为没有start而直接使用stop或者reload报错这个问题: 如果方法一没有解决,使用方法二:-C 指定配置文件nginx.conf或者weblua ...

  2. ES 09 - 定制Elasticsearch的分词器 (自定义分词策略)

    目录 1 索引的分析 1.1 分析器的组成 1.2 倒排索引的核心原理-normalization 2 ES的默认分词器 3 修改分词器 4 定制分词器 4.1 向索引中添加自定义的分词器 4.2 测 ...

  3. ByteView和Sink

    久违啦米娜桑!!! 最近有点忙,一月有余没有更新了,实在对不起大家!!! 上线后看到不少朋友发的私信,感谢大家的赞许与信任,后面我会尽最大的努力按时更新,不断推出更优质的文章!!! 本来计划最后花1讲 ...

  4. 利用SHA-1算法和RSA秘钥进行签名验签(带注释)

    背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...

  5. IT研发工程师职业规划

    一些年过去了,以下关于IT研发工程师纬度的职业规划PPT,仍然有一些可以借鉴: ------------------------------------------------------------ ...

  6. C#根据屏幕分辨率改变图片尺寸

    最近工作中遇到一个问题,就是需要将程序文件夹中的图片根据此时电脑屏幕的分辨率来重新改变图片尺寸 以下为代码实现过程: 1.获取文件夹中的图片,此文件夹名为exe程序同目录下 //读取文件夹中文件 Di ...

  7. 如何在Linux服务器和windows系统之间上传与下载文件

    Do not let dream just be your dream. 背景:Linux服务器文件上传下载. XShell+Xftp安装包(解压即用)百度网盘链接:https://pan.baidu ...

  8. JS添加或删除HTML dom元素的方法实例分析

    本文实例讲述了JS代码添加或删除HTML dom元素的方法.分享给大家供大家参考,具体如下: 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一 ...

  9. Spring注解AOP及单元测试junit(6)

    2019-03-10/20:19:56 演示:将xml配置方式改为注解方式 静态以及动态代理推荐博客:https://blog.csdn.net/javazejian/article/details/ ...

  10. git报错

    -Initial commit Untracked files nothing added to commit but untracked 代码并没有被成功提交,看信息是文件没有被tracked gi ...