最近需要压缩解压问题,查找了一些资料

  1. ZipArchive库,实现对文件的压缩解压缩,网上有资源,可以下载研究一下,对于一些冗余代码可以修改一下。资源链接http://download.csdn.net/detail/deskisme/4440505
  2. iOS zlib文件主要有deflate算法压缩

3.在我所需要的项目中需要解压缩从服务器中请求到的NSData数据(根据自己的情况选择),所有创建了一个NSData类extension 实现对请求到的data的解压缩,此demo代码中有压缩和解压缩两种方法。
可以比较一下zlib 和gzip的区别,压缩率等。(PS在我们查询资源的时候,对于一些东西,更重要的要自己验证一下。fighting^_^)


#import <Foundation/Foundation.h>@interface NSData (STUnzipArchive)
// ZLIB
- (NSData *) zlibInflate;
- (NSData *) zlibDeflate;// GZIP
- (NSData *) gzipInflate;
- (NSData *) gzipDeflate;@end#import "NSData+STUnzipArchive.h"
#include <zlib.h>
@implementation NSData (STUnzipArchive)- (NSData *)zlibInflate
{if ([self length] == 0) return self;NSUInteger full_length = [self length];NSUInteger half_length = [self length] / 2;NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];BOOL done = NO;int status;z_stream strm;strm.next_in = (Bytef *)[self bytes];strm.avail_in = (unsigned)[self length];strm.total_out = 0;strm.zalloc = Z_NULL;strm.zfree = Z_NULL;if (inflateInit (&strm) != Z_OK) return nil;while (!done){// Make sure we have enough room and reset the lengths.if (strm.total_out >= [decompressed length])[decompressed increaseLengthBy: half_length];strm.next_out = [decompressed mutableBytes] + strm.total_out;strm.avail_out = (uint)([decompressed length] - strm.total_out);// Inflate another chunk.status = inflate (&strm, Z_SYNC_FLUSH);if (status == Z_STREAM_END) done = YES;else if (status != Z_OK) break;}if (inflateEnd (&strm) != Z_OK) return nil;// Set real length.if (done){[decompressed setLength: strm.total_out];return [NSData dataWithData: decompressed];}else return nil;
}- (NSData *)zlibDeflate
{if ([self length] == 0) return self;z_stream strm;strm.zalloc = Z_NULL;strm.zfree = Z_NULL;strm.opaque = Z_NULL;strm.total_out = 0;strm.next_in=(Bytef *)[self bytes];strm.avail_in = (uint)[self length];// Compresssion Levels://   Z_NO_COMPRESSION//   Z_BEST_SPEED//   Z_BEST_COMPRESSION//   Z_DEFAULT_COMPRESSIONif (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) return nil;NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chuncks for expansiondo {if (strm.total_out >= [compressed length])[compressed increaseLengthBy: 16384];strm.next_out = [compressed mutableBytes] + strm.total_out;strm.avail_out = (uint)([compressed length] - strm.total_out);deflate(&strm, Z_FINISH);} while (strm.avail_out == 0);deflateEnd(&strm);[compressed setLength: strm.total_out];return [NSData dataWithData: compressed];
}- (NSData *)gzipInflate
{if ([self length] == 0) return self;NSInteger full_length = [self length];NSInteger half_length = [self length] / 2;NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];BOOL done = NO;int status;z_stream strm;strm.next_in = (Bytef *)[self bytes];strm.avail_in = (uint)[self length];strm.total_out = 0;strm.zalloc = Z_NULL;strm.zfree = Z_NULL;if (inflateInit2(&strm, (15+32)) != Z_OK) return nil;while (!done){// Make sure we have enough room and reset the lengths.if (strm.total_out >= [decompressed length])[decompressed increaseLengthBy: half_length];strm.next_out = [decompressed mutableBytes] + strm.total_out;strm.avail_out = (uint)([decompressed length] - strm.total_out);// Inflate another chunk.status = inflate (&strm, Z_SYNC_FLUSH);if (status == Z_STREAM_END) done = YES;else if (status != Z_OK) break;}if (inflateEnd (&strm) != Z_OK) return nil;// Set real length.if (done){[decompressed setLength: strm.total_out];return [NSData dataWithData: decompressed];}else return nil;
}- (NSData *)gzipDeflate
{if ([self length] == 0) return self;z_stream strm;strm.zalloc = Z_NULL;strm.zfree = Z_NULL;strm.opaque = Z_NULL;strm.total_out = 0;strm.next_in=(Bytef *)[self bytes];strm.avail_in = (uint)[self length];// Compresssion Levels://   Z_NO_COMPRESSION//   Z_BEST_SPEED//   Z_BEST_COMPRESSION//   Z_DEFAULT_COMPRESSIONif (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansiondo {if (strm.total_out >= [compressed length])[compressed increaseLengthBy: 16384];strm.next_out = [compressed mutableBytes] + strm.total_out;strm.avail_out = (uint)([compressed length] - strm.total_out);deflate(&strm, Z_FINISH);} while (strm.avail_out == 0);deflateEnd(&strm);[compressed setLength: strm.total_out];return [NSData dataWithData:compressed];
}@end

iOS zlib 压缩解压相关推荐

  1. 使用golang 实现deflate、zlib压缩解压数据

    使用golang 实现deflate.zlib压缩解压数据 在golang的官方标准库的compress目录下,有bzip2.flate.gzip. lzw. zlib包实现对应算法的数据压缩和解压, ...

  2. Delphi Base64编码/解码及ZLib压缩/解压

    最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结: 一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当 ...

  3. Lua 通过 alien 库调用 zlib 压缩/解压

    http://blog.csdn.net/kowity/article/details/7256376 上次的文章(http://blog.csdn.net/kowity/article/detail ...

  4. Qt调用zlib压缩解压文件

    zlib提供compress uncompress 两个函数分别是压缩和解压 ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen ...

  5. c#使用zlib.net压缩解压byte数组

    c#使用zlib.net压缩解压byte数组 简单粗暴直接上代码 遇到的小坑 zlib是提供数据压缩用的函式库 ,本文简单介绍zlib.net解压数组碰到的痛点 简单粗暴直接上代码 public st ...

  6. python zlib_【python】使用zlib进行压缩解压

    A:如何使用zlib模块来进行压缩解压了? Q: 1.使用zlib.compress可以压缩字符串.使用zlib.decompress可以解压字符串. 压缩解压字符串示范代码:import zlibm ...

  7. WebAPI性能优化之压缩解压

    有时候为了提升WebAPI的性能,减少响应时间,我们会使用压缩和解压,而现在大多数客户端浏览器都提供了内置的解压支持.在WebAPI请求的资源越大时,使用压缩对性能提升的效果越明显,而当请求的资源很小 ...

  8. Linux按压缩率大小排序,Linux下常用压缩 解压命令与压缩比率对比

    常用的格式有: tar, tar.gz(tgz), tar.bz2, 不同方式,压缩和解压方式所耗CPU时间和压缩比率也差异也比较大. 1. tar 只是打包动作,相当于归档处理,不做压缩:解压也一样 ...

  9. python压缩文件tar_python 实现tar文件压缩解压的实例详解

    python 实现tar文件压缩解压的实例详解 python 实现tar文件压缩解压的实例详解 压缩文件: import tarfile import os def tar(fname): t = t ...

最新文章

  1. python颜色填充代码_在python tu中用颜色填充形状
  2. C++简易测试代码框架
  3. 论文学习3-Improving Neural Fine-Grained Entity Typing with Knowledge Attention
  4. 计算机专业人毕业设计外文翻译,计算机专业毕业设计外文翻译.doc
  5. webstorm 左侧文件目录树(不显示,怎么办?) - 设置篇
  6. A. Computer Game(纯模拟)
  7. 二分图匹配的判断+p1322+二分图最大匹配+tyvj1035
  8. 每秒 10 万并发的 BI 系统如何频繁发生 Young GC?
  9. webpack打包原理(待续)
  10. 一个类似京东商城那种多条件筛选效果
  11. CentOS 7 (RHEL 7)服务管理命令的变化
  12. HOJ 2739 The Chinese Postman Problem
  13. fullcalendar日历控件知识点集合
  14. 【HAVENT原创】superagentCallback*** is not defined
  15. 《缠中说禅108课》37:背驰的再分辨
  16. Linux Ubuntu 安装五笔输入法 ibus-rime 教程
  17. 目标检测入门常见问题(深度学习 / 图像分类)
  18. Android蓝牙通讯
  19. iTunes 10 选择自定义IPSW
  20. 两个免费的文献翻译网站,支持多种专业翻译,多种语言!

热门文章

  1. C#版 泡泡堂 1.0
  2. java毕业设计-酒店管理系统 酒店预定系统
  3. 工作日志2009年、二
  4. 塔设备设计手册_石油化工设备设计选用手册-塔器(搜索无重复) - 化工设备 - 小木虫 - 学术 科研 互动社区...
  5. 互联网+制造上演两马战:腾讯向左、阿里向右
  6. SAE J2534介绍
  7. 关于opencv更改摄像头参数(帧率,分辨率,曝光度……)的几个问题
  8. Fedora 21 安装 Budgie Desktop
  9. 西邮Linux兴趣小组2021纳新试题
  10. 2020年9月网络安全考试试题