在iPhone开发中实现解压缩gzip
在iPhone开发中实现解压缩gzip是本文要介绍的内容,最近做的一个东西中,需要从网络获取xml文件,但是该文件用了gzip压缩的。搜索一 下有人说gzip压缩的用urlrequest可以自己解压,但是这必须从服务器返回的header中有accept-Encoding说明是gzip 的。也就是用这句就可以实现自解压:
- [urlRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
这个在我的项目中没有作用,因为服务器返回的header中没有Accept-Encoding的说明。这就需要手动解压了!解压需要导入libz.1.2.3.dylib库,导入#import "zlib.h"
下面是解压的代码:
- -(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;
- }
- }
小结:在iPhone开发中实现解压缩gzip的内容介绍完了,希望本文对你有所帮助!
在iPhone开发中实现解压缩gzip的更多相关文章
- iPhone开发中从一个视图跳到另一个视图有三种方法:
iPhone开发中从一个视图跳到另一个视图有三种方法: 1.self.view addSubView:view .self.window addSubView,需要注意的是,这个方法只是把页面加在 ...
- iphone 开发中使用zbar时遇到的几个典型问题解决方法。
iphone 开发中使用zbar时遇到的几个典型问题解决方法. 在近期的一个ios项目中使用到了一个二维码扫描库(Qrcode)--ZBar, 期间遇到2个问题. 1. zbar下载后使用其l ...
- iPhone开发中,关于视图跳转的总结(转)
iPhone开发中,关于视图跳转的总结 iPhone开发中从一个视图跳到另一个视图有三种方法: 1. self.view addSubView:view .self.window addSubView ...
- iphone开发中调用系统打电话功能
iphone开发中调用打电话功能,一般有2种: 1.系统的打电话代码,不返回当前程序: Java代码 [[UIApplication sharedApplication] openURL:[NSURL ...
- iPhone开发中的技巧整理
1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...
- iphone开发中数据持久化之——属性列表序列化(一)
数据持久化是应用程序开发过程中的一个基本问题,对应用程序中的数据进行持久化存储,有多重不同的形式.本系列文章将介绍在iphone开发过程中数据持久化的三种主要形式,分别是属性列表序列号.对象归档化以及 ...
- iphone开发中使用nib(xib)文件的内存管理
iphoneuinavigationcontrollercocoauiviewvariableswindows 在使用nib文件做界面开发的过程中,加载nib文件后,由于设置了outlet和deleg ...
- iphone开发中数据持久化之——嵌入式SQLite(三)
前两篇分别讨论了使用属性列表的数据持久化.使用对象归档的数据持久化,本文将讨论第三个实现数据持久化的方法---嵌入式SQL数据库SQLite3.SQLite3在存储和检索大量数据方面非常有效.它还能够 ...
- iphone开发中数据持久化之——模型对象归档(二)
在Cocoa世界中,术语“归档”是指另一种形式的序列化,它可以实现对任何对象的序列化.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们.只要在类中实现的每个属性都是标量(如 ...
随机推荐
- mysql timestamp为0值时,python读取后的对象为None
MySQL数据表中,如果timestamp类型的字段,值为0, python从数据库读取数据后,得到对象是什么类型,是否为None呢? 下面来测试下. 创建数据表 首先创建数据表,其中字段pr_rul ...
- linux下配置服务自动启动
1.切换到/etc/rc.d/init.d目录下 2.新建脚本,step.sh 3.添加开机启动 chkconfig –add step.sh 4.chkconfig step.sh
- Bootstrap-CSS:目录
ylbtech-Bootstrap-CSS:目录 1.返回顶部 1. 2. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. 2. 6.返回顶部 7.返回顶部 ...
- 自定义application的全局捕获异常实现
package com.loaderman.global; import android.app.Application; import android.os.Environment; import ...
- libvirt报错总结
libvirt 的一些报错总结 出现Permission denied error: internal error process exited while connecting to monitor ...
- delphi 双击dbgrid 调用另一窗体的例子
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...
- idea 中提示:Warning:java: 源值1.5已过时, 将在未来所有发行版中删除
maven的配置文件settings.xml中添加: <profile> <id>jdk-1.8</id> <activation> <activ ...
- SQL:SQL优化
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- 【Linux开发】linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟
linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 关于Typescript - HTMLElement上使用append / prepend函数的问题
因最近在做浏览器打印界面水印的问题,用到后台动态创建标签,样式的处理用到了append,prend函数,Angular build打包的时候却抛出了异常↓ ERROR in src/app/route ...