#import <zlib.h>

压缩

-(NSData *)compressData:(NSData *)uncompressedData
{
if ([uncompressedData length] == 0) return uncompressedData; z_stream strm; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.total_out = 0;
strm.next_in=(Bytef *)[uncompressedData bytes];
strm.avail_in = (unsigned int)[uncompressedData length]; 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 = (unsigned int)([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 *)uncompressZippedData:(NSData *)compressedData
{ if ([compressedData length] == 0) return compressedData; unsigned full_length = [compressedData length]; unsigned half_length = [compressedData length] / 2;
NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
BOOL done = NO;
int status;
z_stream strm;
strm.next_in = (Bytef *)[compressedData bytes];
strm.avail_in = [compressedData 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;
}
}

IOS压缩解压缩的更多相关文章

  1. Linux下的压缩解压缩命令详解

    linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...

  2. Qt之QuaZIP(zip压缩/解压缩)

    简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开 ...

  3. hadoop的压缩解压缩,reduce端join,map端join

    hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...

  4. Linux/centos/redhat下各种压缩解压缩方式详解

    1.zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip -o -d ...

  5. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

  6. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  7. 使用VC++压缩解压缩文件夹

    前言   项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码,  都不是很称心, 现在把我自己实现的代码和大家分享. 要求: 1.使用Unicode(支持中文). 2.使用源代码.(不使用静态或 ...

  8. 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

    原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...

  9. Linux常用命令(第二版) --压缩解压缩命令

    压缩解压缩命令: ----------.gz---------- 1.压缩 gzip[GNU zip]: /bin/gzip 格式: gzip 选项 [文件] #压缩文件,压缩后扩展名为.gz,Lin ...

随机推荐

  1. python--websocket数据解析

    # websocket实现原理 ''' 1.服务端开启socket,监听ip和端口 2.客户端发送连接请求(带上ip和端口) 3.服务端允许连接 4.客户端生成一个随机字符串,和magic strin ...

  2. python 实现经典算法

    import time start_time = time.clock() list_ = [9, 2, 7, 4, 5, 6, 3, 8, 1] """ # 堆排序(通 ...

  3. 推荐一本迷你中文书《JavaScript Promise迷你书(中文版)》

    https://github.com/azu/promises-book http://it-ebooks24.com/ebook/mastering-javascript-promises 传值,调 ...

  4. EntityFramework之多对多关系(四)

    上篇介绍了一对多关系,下面介绍下多对多关系代码编写. 1.新建model实体,User是用户类,Role是角色类,由于是多对多关系,必须得有一个中间类,所以产生了UserRole类 public cl ...

  5. Reporting Service报表水印的添加

    上一篇文章寫到了自帶報表的製作,現在來談談報表水印的添加 1:水印產生代碼 using System; using System.Data; using System.Configuration; u ...

  6. 数学【p2613】 【模板】有理数取余(费马小定理)

    题目描述 给出一个有理数 c=a/b ,求 c mod 19260817的值. 说明 对于所有数据, 0≤a,b≤10^10001 分析: 一看题 这么短 哇简单!况且19260817还是个素数!(美 ...

  7. 洛谷——P1093 奖学金

    P1093 奖学金 题目描述 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分从高到低排序,如果两个同学总分相 ...

  8. 消息队列集群kafka安装配置

    1. 下载wget http://mirror.rise.ph/apache/kafka/0.11.0.0/kafka_2.12-0.11.0.0.tgz2. 安装tar xf kafka_2.12- ...

  9. Binary Tree Iterative Traversal

    Preorder class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ...

  10. 每天一个linxu命令6之jps  查看java进程的端口

    jps -- JavaVirtual Machine Process Status Tool 可以列出本机所有Java进程的pid jps [ options ] [ hostid ] 选项 -q 仅 ...