iPhone开发中实现解压gzip是本文要介绍的内容,最近做的一个东西中,需要从网络获取xml文件,但是该文件用了gzip压缩的。搜索一 下有人说gzip压缩的用urlrequest可以自己解压,但是这必须从服务器返回的header中有accept-Encoding说明是gzip 的。也就是用这句就可以实现自解压

  1. [urlRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

这个在我的项目中没有作用,因为服务器返回的header中没有Accept-Encoding的说明。这就需要手动解压了!解压需要导入libz.1.2.3.dylib库,导入#import "zlib.h"

下面是解压的代码:

  1. -(NSData *)uncompressZippedData:(NSData *)compressedData
  2. {
  3. if ([compressedData length] == 0) return compressedData;
  4. unsigned full_length = [compressedData length];
  5. unsigned half_length = [compressedData length] / 2;
  6. NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
  7. BOOL done = NO;
  8. int status;
  9. z_stream strm;
  10. strm.next_in = (Bytef *)[compressedData bytes];
  11. strm.avail_in = [compressedData length];
  12. strm.total_out = 0;
  13. strm.zalloc = Z_NULL;
  14. strm.zfree = Z_NULL;
  15. if (inflateInit2(&strm, (15+32)) != Z_OK) return nil;
  16. while (!done) {
  17. // Make sure we have enough room and reset the lengths.
  18. if (strm.total_out >= [decompressed length]) {
  19. [decompressed increaseLengthBy: half_length];
  20. }
  21. strm.next_out = [decompressed mutableBytes] + strm.total_out;
  22. strm.avail_out = [decompressed length] - strm.total_out;
  23. // Inflate another chunk.
  24. status = inflate (&strm, Z_SYNC_FLUSH);
  25. if (status == Z_STREAM_END) {
  26. done = YES;
  27. } else if (status != Z_OK) {
  28. break;
  29. }
  30. }
  31. if (inflateEnd (&strm) != Z_OK) return nil;
  32. // Set real length.
  33. if (done) {
  34. [decompressed setLength: strm.total_out];
  35. return [NSData dataWithData: decompressed];
  36. } else {
  37. return nil;
  38. }
  39. }

小结:在iPhone开发中实现解压缩gzip的内容介绍完了,希望本文对你有所帮助!

在iPhone开发中实现解压缩gzip的更多相关文章

  1. iPhone开发中从一个视图跳到另一个视图有三种方法:

    iPhone开发中从一个视图跳到另一个视图有三种方法:   1.self.view addSubView:view .self.window addSubView,需要注意的是,这个方法只是把页面加在 ...

  2. iphone 开发中使用zbar时遇到的几个典型问题解决方法。

    iphone 开发中使用zbar时遇到的几个典型问题解决方法.   在近期的一个ios项目中使用到了一个二维码扫描库(Qrcode)--ZBar, 期间遇到2个问题.   1. zbar下载后使用其l ...

  3. iPhone开发中,关于视图跳转的总结(转)

    iPhone开发中,关于视图跳转的总结 iPhone开发中从一个视图跳到另一个视图有三种方法: 1. self.view addSubView:view .self.window addSubView ...

  4. iphone开发中调用系统打电话功能

    iphone开发中调用打电话功能,一般有2种: 1.系统的打电话代码,不返回当前程序: Java代码 [[UIApplication sharedApplication] openURL:[NSURL ...

  5. iPhone开发中的技巧整理

    1.NSCalendar用法 -(NSString *) getWeek:(NSDate *)d { NSCalendar *calendar = [[NSCalendar alloc] initWi ...

  6. iphone开发中数据持久化之——属性列表序列化(一)

    数据持久化是应用程序开发过程中的一个基本问题,对应用程序中的数据进行持久化存储,有多重不同的形式.本系列文章将介绍在iphone开发过程中数据持久化的三种主要形式,分别是属性列表序列号.对象归档化以及 ...

  7. iphone开发中使用nib(xib)文件的内存管理

    iphoneuinavigationcontrollercocoauiviewvariableswindows 在使用nib文件做界面开发的过程中,加载nib文件后,由于设置了outlet和deleg ...

  8. iphone开发中数据持久化之——嵌入式SQLite(三)

    前两篇分别讨论了使用属性列表的数据持久化.使用对象归档的数据持久化,本文将讨论第三个实现数据持久化的方法---嵌入式SQL数据库SQLite3.SQLite3在存储和检索大量数据方面非常有效.它还能够 ...

  9. iphone开发中数据持久化之——模型对象归档(二)

    在Cocoa世界中,术语“归档”是指另一种形式的序列化,它可以实现对任何对象的序列化.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们.只要在类中实现的每个属性都是标量(如 ...

随机推荐

  1. mysql timestamp为0值时,python读取后的对象为None

    MySQL数据表中,如果timestamp类型的字段,值为0, python从数据库读取数据后,得到对象是什么类型,是否为None呢? 下面来测试下. 创建数据表 首先创建数据表,其中字段pr_rul ...

  2. linux下配置服务自动启动

    1.切换到/etc/rc.d/init.d目录下 2.新建脚本,step.sh 3.添加开机启动   chkconfig –add step.sh 4.chkconfig step.sh

  3. Bootstrap-CSS:目录

    ylbtech-Bootstrap-CSS:目录 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1.   2.   6.返回顶部   7.返回顶部 ...

  4. 自定义application的全局捕获异常实现

    package com.loaderman.global; import android.app.Application; import android.os.Environment; import ...

  5. libvirt报错总结

    libvirt 的一些报错总结 出现Permission denied error: internal error process exited while connecting to monitor ...

  6. delphi 双击dbgrid 调用另一窗体的例子

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  7. idea 中提示:Warning:java: 源值1.5已过时, 将在未来所有发行版中删除

    maven的配置文件settings.xml中添加: <profile> <id>jdk-1.8</id> <activation> <activ ...

  8. SQL:SQL优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  9. 【Linux开发】linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟

    linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  10. 关于Typescript - HTMLElement上使用append / prepend函数的问题

    因最近在做浏览器打印界面水印的问题,用到后台动态创建标签,样式的处理用到了append,prend函数,Angular build打包的时候却抛出了异常↓ ERROR in src/app/route ...