logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

iOS , MacOS, iPhone용 GZipStream class 구현하기

by digipine posted Nov 01, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
아이폰/아이패드와 안드로이드, 윈도우간의 압축 통신 프로토콜을 구현하다보면 몇가지 문제가 발생합니다.
자바나 C# 은 기본적으로 포함된 GZipStream 클래스를 사용하는데
IOS나 OSX의 경우는 zlib 기반의 라이브러리를 사용해서 상대의 압축된 데이터 해제시에  Z_DATA_ERROR를 리턴합니다.
 
조사해보니 window bits의 값이 달라서 발생하는 문제입니다.
아래 코드와 같이 기본값인 15에 압축시에는 16을 더하고 해제시에는 32를 더해주면 GZipStream와 잘 호완이 됩니다.
잘 찾아보니 zlib 매뉴얼 설명이 되어있군요... 한참을 고생했습니다. -_-; 다들 참고하세요.
 
첨부 : gzip의 개발자들도 Stream 구현시에는 zlib을 사용하라고 권장하고 있는데요... 아마도 RFC 때문인것 같습니다.
 
아래 소스는 NSData를 이용하는데 각작의 상황에 맞게 고쳐서 사용하시면됩니다.
 
@interface NSData (DDData)
 
// gzip compression utilities
- (NSData *)gzipInflate;
- (NSData *)gzipDeflate;
 
@end
 
 
 
-----------------------------------------
 
 
 
#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;
}
 
- (NSData *)gzipDeflate
{
 if ([self length] == 0) return self;
 
 z_stream strm;
 
 strm.zalloc = Z_NULL;
 strm.zfree = Z_NULL;
 strm.opaque = Z_NULL;
 strm.total_out = 0;
 strm.next_in=(Bytef *)[self bytes];
 strm.avail_in = [self length];
 
 // Compresssion Levels:
 //   Z_NO_COMPRESSION
 //   Z_BEST_SPEED
 //   Z_BEST_COMPRESSION
 //   Z_DEFAULT_COMPRESSION
 
 if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;
 
 NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansion
 
 do {
  
  if (strm.total_out >= [compressed length])
   [compressed increaseLengthBy: 16384];
  
  strm.next_out = [compressed mutableBytes] + strm.total_out;
  strm.avail_out = [compressed length] - strm.total_out;
  
  deflate(&strm, Z_FINISH);  
  
 } while (strm.avail_out == 0);
 
 deflateEnd(&strm);
 
 [compressed setLength: strm.total_out];
 return [NSData dataWithData:compressed];
}
 
@end
TAG •

List of Articles
No. Subject Author Date Views
55 iOS - Objective C 정규식 사용법 2 digipine 2017.11.01 961
54 iOS - Objective - C 정규식 사용하기 digipine 2017.11.01 470
53 iOS - Objective - C, URL 인코딩과 디코딩 digipine 2017.11.01 1653
52 iOS,OSX - CFSocket 사용법 digipine 2017.11.01 443
51 iOS - NSString 와 NSData 간의 데이터 상호 변환 digipine 2017.11.01 249
50 iOS - OpenURL으로 HTML에서 어플 실행 digipine 2017.11.01 565
49 iOS - Thread Loop 내에서 UI 업데이트 방법 digipine 2017.11.01 211
48 iOS - NSURLConnection로 다중 다운로드 구현 digipine 2017.11.01 333
47 iOS - Query string을 Decode 하는 소스 digipine 2017.11.01 327
46 iOS - Objective-C 남아있는 메모리 공간 확인 방법 digipine 2017.11.01 215
45 iOS - 코드 수행시간 측정하기 - getTickCount digipine 2017.11.01 846
44 iOS - BSD Socket 네트워크 프로그래밍 digipine 2017.11.01 662
43 iOS - Socket Nagle 알고리듬 OFF digipine 2017.11.01 208
42 iOS - NSString의 언어 인코딩 메소드 정리 digipine 2017.11.01 714
41 iOS - Objective-C Callback for C++ digipine 2017.11.01 266
40 iOS - Openssl 빌드하기 digipine 2017.11.01 946
39 iOS - View 이동 전환 하기 총정리 digipine 2017.11.01 537
» iOS , MacOS, iPhone용 GZipStream class 구현하기 digipine 2017.11.01 357
37 iOS - Sleep Mode Blocking 방법, 앱실행시 슬립모드 진입 방지 digipine 2017.11.01 572
36 iOS - sizeWithFont 메소드 deprecated와 sizeWithAttributes digipine 2017.11.02 610
Board Pagination Prev 1 2 3 Next
/ 3