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
35 iOS - 코드 수행시간 측정하기 - getTickCount digipine 2017.11.01 846
34 OpenAL PDF, Sample Source file digipine 2017.11.02 796
33 WatermelonDB 'jsi/jsi.h' file not found 문제 해결 file digipine 2021.04.06 732
32 iOS - NSString의 언어 인코딩 메소드 정리 digipine 2017.11.01 714
31 XCode 8 업데이트 후 Code Sign Error 발생 시 해결법 1 digipine 2017.11.02 711
30 The distance estimate iBeacon signal strength lizard2019 2019.10.25 695
29 macOS ARP Spoofing Attack file digipine 2020.09.17 692
28 [iOS, MacOS] NSArray 정렬 Sorting에 대해서 digipine 2017.11.02 663
27 iOS - BSD Socket 네트워크 프로그래밍 digipine 2017.11.01 662
26 iOS - UILabel 에서 AttributeString 사용하기 digipine 2017.11.02 640
25 iOS - sizeWithFont 메소드 deprecated와 sizeWithAttributes digipine 2017.11.02 610
24 iOS - Sleep Mode Blocking 방법, 앱실행시 슬립모드 진입 방지 digipine 2017.11.01 572
23 [iOS, MacOS] ATS 보안 정책 가이드 digipine 2017.11.02 567
22 iOS - OpenURL으로 HTML에서 어플 실행 digipine 2017.11.01 565
21 [MacOS] Terminal 에서 zsh compinit: insecure directories 경고 제거하기 lizard2019 2021.04.30 564
20 [iOS] Audio Session Setting digipine 2021.11.26 548
19 iOS - View 이동 전환 하기 총정리 digipine 2017.11.01 537
18 [macOS] Xcode 디버깅 시 Could not attach pid 오류 해결 file lizard2019 2023.06.05 507
17 [iOS] 개발자를 위한 iOS 15의 새로운 기능 file digipine 2021.11.04 497
16 iOS - Objective - C 정규식 사용하기 digipine 2017.11.01 470
Board Pagination Prev 1 2 3 Next
/ 3