- 浏览: 37996 次
- 性别:
- 来自: 北京
最新评论
to allow zlib compression/decompression of NSData. But a couple months ago I went looking for similar code to allow gzip compression/decompression. At the time I didn't find any, and found myself wading through the zlib documentation and header files until I came up with a solution. I've recently noticed that someone else on CocoaDev has also mentioned the solution, but it looks like they didn't fully implement a deflate version of it. So, without further ado, here is an NSData category that adds support for gzip compression and decompression:
@interface NSData (DDData)
// gzip compression utilities
- (NSData *)gzipInflate;
- (NSData *)gzipDeflate;
@end
-----------------------------------------
#include
@implementation NSData (DDData)
- (NSData *)gzipInflate
{
if ([self length] == 0) return self;
unsigned full_length = [self length];
unsigned 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 = [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 = [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 = [self length];
// Compresssion Levels:
// Z_NO_COMPRESSION
// Z_BEST_SPEED
// Z_BEST_COMPRESSION
// Z_DEFAULT_COMPRESSION
if (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 expansion
do {
if (strm.total_out >= [compressed length])
[compressed increaseLengthBy: 16384];
strm.next_out = [compressed mutableBytes] + strm.total_out;
strm.avail_out = [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
转自 http://hi.baidu.com/leonqgl/blog/item/abe7eec7f9ac4dc6d10060b1.html
@interface NSData (DDData)
// gzip compression utilities
- (NSData *)gzipInflate;
- (NSData *)gzipDeflate;
@end
-----------------------------------------
#include
@implementation NSData (DDData)
- (NSData *)gzipInflate
{
if ([self length] == 0) return self;
unsigned full_length = [self length];
unsigned 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 = [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 = [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 = [self length];
// Compresssion Levels:
// Z_NO_COMPRESSION
// Z_BEST_SPEED
// Z_BEST_COMPRESSION
// Z_DEFAULT_COMPRESSION
if (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 expansion
do {
if (strm.total_out >= [compressed length])
[compressed increaseLengthBy: 16384];
strm.next_out = [compressed mutableBytes] + strm.total_out;
strm.avail_out = [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
转自 http://hi.baidu.com/leonqgl/blog/item/abe7eec7f9ac4dc6d10060b1.html
发表评论
-
IOS
2012-06-08 18:08 2149转自 http://site.douban.com/ ... -
iPhone4文件结构分析列表
2012-06-06 18:25 11、 【/Applications】 ... -
IOS 入门开发之使用Xcode4制作静态库详解
2012-05-23 16:40 1163雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原 ... -
NSError code错误列表信息(NSFileManage、NSURL...)
2012-05-14 13:05 2563NSError codes in the Cocoa erro ... -
iOS4中Core Motion框架的介绍和使用
2012-04-18 20:47 894http://www.kunli.info/2010/07/3 ... -
iOS5新开发的API总述——WWDC 2011
2012-03-23 13:03 1774转自 http://alloc.sinaapp.com ... -
中文版多线程编程指南
2012-03-19 19:33 923转自http://www.cocoachina.com/iph ... -
iPhone 路径大全
2012-03-13 11:41 9411、【/Applications】 常用 ... -
App Store审核指南
2012-03-08 19:19 936前言 感谢您付出宝 ... -
iOS设备型号与产品名称的对应
2012-03-07 21:47 2319"iPhone1,1" => &qu ... -
iOS平台开发者总结出的宝贵上架经验
2012-03-06 16:06 1059以下是许多iOS平台开发者总结出的宝贵上架经验: 基本 ... -
版本控制清除所有.svn目录
2012-02-29 18:04 1537删除这些目录是很简单的,命令如下find . -type d ... -
UIView的一些基本方法理解:loadView、viewDidLoad、viewDidUnload、viewWillAppear、viewWillDisapp
2012-02-29 15:51 1797对于其它几个基本方法就不那么常用了,为了更好地编程使用这些方法 ... -
iphone音乐播放
2012-02-28 14:20 1224在 iPhone 应用或者是游戏的开发过程中,对声音的支持是必 ... -
Zlib的deflate, z_stream结构的使用
2012-02-20 22:52 3642Zlib的deflate, z_stream结构的 ... -
iphone通讯录AddressBook.framework和AddressBookUI.framework的应用
2012-02-02 12:26 3160通讯录中联系人相关的应用iPhone提供了两个框架:Ad ...
相关推荐
iphone文件解压缩zip-gzip zip是对文件操作,gzip在内存中对data进行操作的
gzip.dll 文件压缩解压缩需要的必要文件文件压缩解压缩需要的必要文
文件gzip解压缩,vs开发(如果文件时由pcap抓包还原的文件,需要将16进制显示的该文件的1f8b之前的数据删除)
VS2005开发,很不错的,使用GZip解压缩文件.rar
网上相关资源多半零碎不可用,详细整理了下的文件Gzip压缩后进行AES加密,同时反向解密解压缩。java代码和需要的jar包亲测可用!!
一个Java随书实例:用GZIP压缩解压文件,从源文件得到文件输入流,得到目标文件输出流,得到压缩输出流,设定读入缓冲区尺寸,弹出文件选择器,并判断是否点击了打开按钮,判断事件来自于哪个按钮,用于选择解压和...
调用net自带的方法,进行解压缩,对小文件效果理想.
可以使用GZIP解压缩文件 代码用visual studio 2005编写 只需要改一点就可以用到你的程序中
gzip -- 支持gzip文件 这个模块提供了一些简单的接口来对文件进行压缩和解压缩,类似于GNU项目中得gzip 数据的压缩源于zlib模块的支持。 在gzip模块中提供了GzipFile类,在该类中提供了像open( ), compress( ), ...
JAVA文件压缩与解压缩实践报告 主函数 gzip压缩模块代码 压缩模块要完成的就是将文件读入以后进行压缩,再将压缩后的数据写入一个新的文件,其部分代码如下: public class gzip { public static void main(String...
大家平时用易语言访问部分网站时发现源码不正常显示,为空或只有一个问题,一般是因为需要解压缩,并且需要gzip.dll文件,有了这个模块,就省事多了,直接调用并且不写出gzip.dll,防止杀软误报
2 压缩与解压缩程序分析 2.1 需求分析 2.2 使用的算法理论 2.2.1 LZ77算法简介 2.2.2 Huffman算法简介 2.2.3 GZIP算法原理分析 2.3 开发环境 3 总体设计 3.1 程序功能模块 3.2 模块分析与流程图 3.2.1 压缩...
文件解压缩:将压缩文件解压缩成原始文件,恢复文件的原始内容和结构。 支持多种压缩格式:Java提供了对多种常见压缩格式的支持,如ZIP、GZIP等。 异常处理:处理压缩和解压缩过程中可能出现的异常,保证程序的稳定...
2.Gzip是将文件的存储空间压缩保存,文件名后缀是.gz 3.tar.gz或.tgz通常是指将文件打包到一个tar文件中,并将它使用Gzip进行压缩。 一、将两个文件打包到tar.gz 下面的这个例子是将2个文件打包为tar.gz压缩文件。...
前端gzip 解压缩pako.min.js文件
VC++ MFC 如何用 CZip CUnzip类压缩解压缩文件 包括源代码和Example文件,还有说明文件~
2 压缩与解压缩程序分析 2.1 需求分析 2.2 使用的算法理论 2.2.1 LZ77算法简介 2.2.2 Huffman算法简介 2.2.3 GZIP算法原理分析 2.3 开发环境 3 总体设计 3.1 程序功能模块 3.2 模块分析与流程图 3.2.1 压缩...
GZIP加密解密解压缩工具示例代码 1. 基于LZ77算法通过“滑动窗口”的方法对数据上报文件进行压缩处理; 2. 对得到的数据文件的结果再使用Huffman编码的方法进行压缩。 通过国密SM4加密解密
使用Gzip打包JavaScript、CSS文件,Java后台Filter解压缩