日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java ios压缩_iOS与Java服务器GZip压缩问题【转】

發布時間:2023/12/15 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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压缩问题【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。