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
56 [macOS, iOS] 개발자 정보 확인하는 명령어 digipine 2023.03.23 604
55 iOS - Socket Nagle 알고리듬 OFF digipine 2017.11.01 681
54 iOS - Objective-C 남아있는 메모리 공간 확인 방법 digipine 2017.11.01 702
53 macOS Daemon 관련 시스템 폴더 목록 lizard2019 2024.03.08 708
52 Core Audio를 사용하여 macOS에서 Audio를 Capture하는 코드 digipine 2024.04.19 734
51 iOS - Thread Loop 내에서 UI 업데이트 방법 digipine 2017.11.01 777
50 iOS - Objective-C Callback for C++ digipine 2017.11.01 780
49 Apple AppStore App Review 시 Reject 피하기 위한 방법 digipine 2017.11.02 807
48 Firebase 'GoogleUtilities/GULURLSessionDataResponse.h' file not found Error Fix lizard2019 2023.07.04 818
47 [iOS] Audio Session Setting digipine 2021.11.26 824
46 Concurrent vs Serial DispatchQueue: Concurrency in Swift explained lizard2019 2021.04.16 828
45 [macOS] 현재 사용 중인(열려있는) 포트 확인하고 Close 하기 digipine 2022.10.24 853
44 iOS - NSURLConnection로 다중 다운로드 구현 digipine 2017.11.01 858
43 iOS - NSString 와 NSData 간의 데이터 상호 변환 digipine 2017.11.01 876
42 iOS - Query string을 Decode 하는 소스 digipine 2017.11.01 924
41 [iOS] 개발자를 위한 iOS 15의 새로운 기능 file digipine 2021.11.04 929
40 [MacOS] Terminal 에서 zsh compinit: insecure directories 경고 제거하기 lizard2019 2021.04.30 969
» iOS , MacOS, iPhone용 GZipStream class 구현하기 digipine 2017.11.01 998
38 iOS - View 이동 전환 하기 총정리 digipine 2017.11.01 1029
37 WatermelonDB 'jsi/jsi.h' file not found 문제 해결 file digipine 2021.04.06 1072
Board Pagination Prev 1 2 3 Next
/ 3