自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在iPhone開發(fā)中實(shí)現(xiàn)解壓縮gzip

移動(dòng)開發(fā) iOS
本文介紹的是在iPhone開發(fā)中實(shí)現(xiàn)解壓縮gzip,主要介紹的是解壓縮的操作,我們來看內(nèi)容。

iPhone開發(fā)中實(shí)現(xiàn)解壓gzip是本文要介紹的內(nèi)容,最近做的一個(gè)東西中,需要從網(wǎng)絡(luò)獲取xml文件,但是該文件用了gzip壓縮的。搜索一 下有人說gzip壓縮的用urlrequest可以自己解壓,但是這必須從服務(wù)器返回的header中有accept-Encoding說明是gzip 的。也就是用這句就可以實(shí)現(xiàn)自解壓

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

這個(gè)在我的項(xiàng)目中沒有作用,因?yàn)榉?wù)器返回的header中沒有Accept-Encoding的說明。這就需要手動(dòng)解壓了!解壓需要導(dǎo)入libz.1.2.3.dylib庫,導(dǎo)入#import "zlib.h"

下面是解壓的代碼:

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

小結(jié):在iPhone開發(fā)中實(shí)現(xiàn)解壓縮gzip的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2023-01-30 09:04:56

Linux命令unzip

2018-09-14 16:18:26

Linux壓縮文件應(yīng)用程序

2010-06-24 09:29:02

Linux Bzip2

2012-05-10 09:43:28

2010-01-04 09:27:31

Linux壓縮解壓縮命令詳解

2023-12-21 07:30:36

PythonZipfileTarfile

2010-03-05 09:50:37

Ubuntu ligh

2023-03-29 08:59:59

Go壓縮包文檔

2009-10-21 09:10:52

VB.NET壓縮

2011-07-26 14:18:20

2016-11-17 22:02:13

Linux壓縮及解壓縮

2010-06-24 10:42:42

Bzip2壓縮

2021-02-22 07:58:52

Linux壓縮解壓

2009-12-08 16:33:45

PHP unpack函

2011-08-15 14:07:53

Objective-C解壓縮ZIP文件

2024-02-22 12:16:55

Python壓縮數(shù)據(jù)

2013-07-22 13:54:32

iOS開發(fā)ASIHTTPRequ

2009-07-08 16:10:36

Servlet和JSPJSP頁面

2018-01-12 17:03:29

HTTPgzip壓縮

2022-01-26 07:35:10

爬蟲Requestsgzip
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)