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
36 iOS - Socket Nagle 알고리듬 OFF digipine 2017.11.01 208
35 iOS - Thread Loop 내에서 UI 업데이트 방법 digipine 2017.11.01 211
34 iOS - UILabel 에서 AttributeString 사용하기 digipine 2017.11.02 640
33 iOS - UITextField의 Placeholder Color 색 변경하기 file digipine 2017.11.02 1152
32 iOS - View 이동 전환 하기 총정리 digipine 2017.11.01 537
31 iOS - 코드 수행시간 측정하기 - getTickCount digipine 2017.11.01 848
30 iOS,OSX - CFSocket 사용법 digipine 2017.11.01 444
29 macOS ARP Spoofing Attack file digipine 2020.09.17 694
28 macOS Daemon 관련 시스템 폴더 목록 lizard2019 2024.03.08 28
27 MacOS mysql 비밀번호 분실 시 재설정하기 digipine 2017.11.14 1029
26 OpenAL PDF, Sample Source file digipine 2017.11.02 796
25 OSX - Screen Serial Terminal - OSX에서 시리얼 터미널 사용하기 digipine 2017.11.03 6871
24 OSX 사파리 최신 버전 폰트 변경하기 file digipine 2017.11.03 1224
23 The distance estimate iBeacon signal strength lizard2019 2019.10.25 695
22 WatermelonDB 'jsi/jsi.h' file not found 문제 해결 file digipine 2021.04.06 732
21 XCode 8 업데이트 후 Code Sign Error 발생 시 해결법 1 digipine 2017.11.02 711
20 [iOS, MacOS] ATS 보안 정책 가이드 digipine 2017.11.02 570
19 [iOS, MacOS] NSArray 정렬 Sorting에 대해서 digipine 2017.11.02 667
18 [iOS, MacOS] NSNotification, NSNotificationCenter 사용법 digipine 2017.11.02 1026
17 [iOS, MacOS] Singleton 싱글톤 패턴 사용하기 2 digipine 2017.11.02 1080
Board Pagination Prev 1 2 3 Next
/ 3