java ios压缩_iOS与Java服务器GZip压缩问题【转】
昨天搞了一天的GZip压缩,试了三种方式(libz库,ZipArchive,ASIHttpRequest),一开始都不成功。
理论上三个应该都能用的,但我都不行。等我试到第三种方式的时候才知道,不是我的问题,而是后台的问题(Java端输出方式一会再说)。
今天就总结一下,写写iOS与Java服务器获取压缩数据的方法吧。
一、客户端-服务端数据压缩解压流程(ASIHttpRequest)
客户端生成request,设置header允许使用压缩("Accept-Encoding","gzip"),即是告诉服务器,客户端支持压缩,但凡
可以压缩的服务器,尽管来吧!服务器收到这个header,如果它支持压缩,可以通过压缩方式输出数据,然后再写入response的
header("Content-Encoding","gzip")
1.以ASIHttpRequest为例,代码如下:
NSURL*
requestURL = [NSURL URLWithString:_listURL];
ASIHTTPRequest *request = [ASIHTTPRequest
requestWithURL:requestURL];
// 默认为YES,
你可以设定它为NO来禁用gzip压缩
[request
setAllowCompressedResponse:YES];
[request
setDelegate:self];
[request
startAsynchronous];
如果是普通的URLRequest,只要:
request.setHeader("Accept-Encoding","gzip");
2.服务器端返回:
response.setHeader("Content-Encoding","gzip");
3.客户端响应,同样以ASIHttpRequest为例(此例传输的是json数据,我还使用了SBJson解析一下):
- (void)requestFinished:(ASIHTTPRequest *)request{
NSString
*jsonString = @"";
SBJsonParser* jsonParser = [[SBJsonParser alloc] init];
NSMutableDictionary *jsonDictionary = nil;
BOOL
dataWasCompressed = [request isResponseCompressed]; //
响应是否被gzip压缩过?
if
(dataWasCompressed) {
NSData *uncompressedData = [request responseData]; // 解压缩后的数据
NSStringEncoding enc =
CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
jsonString = [[NSString alloc]initWithData:uncompressedData
encoding:enc];
jsonDictionary = [jsonParser objectWithString:jsonString
error:nil];
[jsonString release];
} else
{
jsonString = [request responseString];
jsonDictionary = [jsonParser objectWithString:jsonString
error:nil];
}
self._tableDict = jsonDictionary;
[jsonParser
release];
[self
loadTableDict];
[self
release];
}
附上一篇非常详细的ASIHttpRequest请求Json数据教程(无GZip相关内容):
http://ios-blog.co.uk/articles/tutorials/parsing-json-on-ios-with-asihttprequest-and-sbjson/
libz库
libz库是官方的一个库,貌似ASIHttpRequest也是用这个库解压的,当我们获得压缩过的data数据以后(方法与上面类似,只是获得了普通
的data数据响应),可以使用这种方法解压数据,解压方法如下所示(如果仅仅放在当前类下面使用,传个data参数进来,然后把self换成变量名):
#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;
}
附上一篇非常详细的libz库压缩教程
http://www.clintharris.net/2009/how-to-gzip-data-in-memory-using-objective-c/
以及压缩解压教程(代码从这里拷贝的):
http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html
ZipArchive
上面讲的都是Memory压缩与解压,ZipArchive主要是对文档进行处理。
昨天在上述方法不成功的情况下,我把获取的data数据savetofile,然后再处理,理论上是可行的,但是由于服务器有误,获取的数据不对,所以我怎么都解压不成功!!!!
示例如下:
Objective-C class used to zip / unzip compressed zip file.
Usage:
Add all the files to you project, and and framework
libz.1.2.3.dylib.
include ZipArchive.h using #import "ZipArchive/ZipArchive.h"
* create zip file
ZipArchive* zipFile = [[ZipArchive alloc] init];
[zipFile CreateZipFile2:@"zipfilename"]; // A
OR
[[zipFile CreateZipFile2:@"zipfilename" Password:@"your
password"];// if password needed,
//empty password will get same
result as A
[zipFile addFileToZip:@"fullpath of the file" newname:@"new name of
the file without path"];
....add any number of files here
[zipFile CloseZipFile2];
[zipFile release]; // remember to release the object
* unzip compressed file
ZipArchive* zipFile = [[ZipArchive alloc] init];
[zipFile UnzipOpenFile:@"zip file name"]; // B (the zip got no
password)
OR
[zipFile UnzipOpenFile:@"zip file name" Password:@"password"
];
[zipFile UnzipFileTo:@"output path" overwrite:YES];
[zipFile UnzipCloseFile];
[zipFile release];
java ios压缩_iOS与Java服务器GZip压缩问题【转】相关推荐
- java ios压缩_iOS与Java服务器GZip压缩问题
昨天搞了一天的GZip压缩,试了三种方式(libz库,ZipArchive,ASIHttpRequest),一开始都不成功.理论上三个应该都能用的,但我都不行.等我试到第三种方式的时候才知道,不是我的 ...
- java ios乱码_iOS发送邮件及其中文乱码解决方法
引 要我说呀,你如果不曾碰到奇奇怪怪的需求都不好意思说你是个程序猿.最近我碰到了,所以才有这篇文章,记录一下过程中遇到的问题. 这篇文章是记录一下我是如何在iOS端实现自动发送邮件功能的.某个应用场景 ...
- php 使用压缩css文件,PHP-使用GZIP压缩静态CSS文件
所以我有一个CSS文件style.css.在同一目录中,我有images /文件夹. 如何制作一个从另一个文件夹压缩style.css的脚本? 现在我有这个: if(isset($_GET['css' ...
- java gzip压缩json_spring boot 设置 gzip 压缩
为了减少数据在网络中的传输量,从而减少传输时长,增加用户体验,浏览器大都是支持Gzip压缩技术的,http的请求头 Accept-Encoding:gzip, deflate 就表示这次请求可以接受G ...
- web性能优化--用gzip压缩资源文件
#一.gzip压缩技术 gzip(GNU- ZIP)是一种压缩技术.经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会快得多.gzip的压缩页面需要浏览器和服务器双 ...
- 最简单易懂的Gzip压缩实现,最清晰的OkHttp的Gzip压缩详解
Gzip压缩和解压的实现 Gzip压缩使用起来很简单,以前我也只是在客户端使用,服务器端不用管,所以我只用过GZIPInputStream来读取,用起来也没有问题.后来OkHttp开始流行,后来听说O ...
- 启用Gzip压缩(IIS)提高客户端网站访问速度
IIS上启用Gzip压缩(HTTP压缩) 详解 一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联 ...
- 在IIS上启用Gzip压缩 (HTTP压缩)方法
本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小 ...
- Wordpress 提速之 Gzip 压缩
今天来聊下 wordpress 提速,其实关于这方面的话题网上其实蛮多的,速度对一个网站来说无疑是非常重要的,对于速度的追求也是无止境的,在这方面的表率无疑就是 shawn 了,看他博客的很多技巧都是 ...
最新文章
- 想在SqlDbHelper.cs类中加的垃圾方法
- Android网络连接判断与处理
- win10 输入法不见了、繁体 问题解决
- 精品网摘:大内核锁将何去何从
- xampp的Apache无法启动解决方法
- JDK内置工具--jconsole
- hdu 4352 XHXJ's LIS
- 同步降压DC-DC转换IC——XC9264
- RocksDB 6.0.2 发布,Facebook 推出的存储系统
- Windows Mobile系统弹出输入法时,自动调整窗口显示
- mysql五-1:单表查询
- vs2008打开vs2010所做的项目的方法
- 该文件可能是只读的 或者您要访问的位置_Linux应用编程之文件操作 系统调用篇(下)...
- SQL2K数据库开发七之表操作添加删除和修改列
- 【数学建模】预测模型——多元回归分析 SPSS实现
- 74hc138译码器实验c语言程序,实验二74HC138译码器实验学生
- 算法进阶之BFS 算法
- CT图像去除金属伪影-MATLAB实现
- 如何删除 Windows 10 上的 Windows.old 文件夹?
- Apple Pay 究竟是什么
热门文章
- ios15 LJScrollPageVC第三方框架的使用
- IOS仿微信键盘快捷工具栏
- java ftp主动模式和被动模式_ftp主动模式和被动模式
- 如何实现移动端轮播图的左滑右滑效果
- 南京大学计算机科学与技术学费,南京大学计算机科学与技术系招生信息
- python mp4 切片_FFmpeg MP4视频切片成TS m3u8播放 好文收集
- 导入第三方依赖库slidingmenu
- java数据段 静态区_linux进程的堆栈空间_代码段(指令,只读)、数据段(静态变量,全局变量)、堆栈段(局部变量)、栈【转】...
- springboot日志写入mysql_44. Spring Boot日志记录SLF4J【从零开始学Spring Boot】
- 使用apache配置基于IP地址的虚拟主机