Microsoft MVP성태의 닷넷 이야기
.NET Framework: 135. C# - Deflate, GZip, Zip [링크 복사], [링크+제목 복사],
조회: 34789
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 2개 있습니다.)
.NET Framework: 135. C# - Deflate, GZip, Zip
; https://www.sysnet.pe.kr/2/0/723

.NET Framework: 136. 자바와 닷넷의 압축 호환
; https://www.sysnet.pe.kr/2/0/724





C# - Deflate, GZip, Zip


보통 압축 알고리즘을 다루다 보면 쉽게 볼 수 있는 단어들이 있는데요. 각각의 차이점에 대해서 다음의 글에 간략하게 설명되어 있습니다.

Re: Difference between gzip, zip, deflate
; http://newsgroups.derkeiler.com/Archive/Comp/comp.compression/2007-07/msg00011.html

정리해 보면 대략 다음과 같은 정도로만 알아둬도 될 것 같습니다.

  • Deflate = 원시 압축 데이터
  • GZip = Header + Deflate 데이터 + Footer
  • Zip = 다양한 압축 포맷, 다중 파일 지원

그렇다면, 닷넷으로는 이를 어떻게 구현할까요?

우선, Deflate는 DeflateStream으로 .NET 2.0에서부터 지원되고 있습니다. 그런 의미에서 DeflateStream은 Header/Footer에 대한 부가적인 데이터를 포함하지 않고 LZ77/허프만 코딩이 적용되어 산출된 원시 압축 데이터만을 다루기 때문에 즉석으로 사용할 수 있는 용도로 적합합니다. 아쉬운 점이 있다면 다양한 압축률 지정이 안 된다는 것입니다. 게다가 내부적으로 Huffman_only 방식으로만 사용할지에 대한 옵션 및 그에 대한 구현 방법도 명시적으로 공개되어 있지 않습니다.

구체적인 사용 예제는 MSDN 도움말에 잘 공개되어 있습니다. (도움말에도 나오지만, 4GB 이상의 파일에는 적용할 수 없습니다.)

.NET Framework Class Library - DeflateStream Class
; https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.deflatestream

특이한 것은, 보통 일반적인 Deflate 라이브러리 같은 데에서 보면 압축은 Deflate, 압축 해제는 Inflate라는 접두어를 써서 표현하고는 하는데 .NET의 경우에는 2가지 모두 단일하게 DeflateStream의 생성자에서 CompressionMode를 지정하는 것으로 해결하고 있습니다.

다음으로 GZip은, 여러분들이 많이 써온 GZipStream과 비교됩니다. 위에 설명한 대로 Deflate 압축 데이터와 함께 앞/뒤에 부가적인 메타 정보를 포함합니다. 재미있는 것은 GZipStream이 말 그대로 "Stream" 차원에서 사용될 수 있도록 구현되었기 때문에 "하나의 파일"을 압축해서 보관하는 일반적인 ZIP 실행 파일에서 다뤄지는 헤더를 모두 포함하지는 않습니다. 이에 관해서는 다음의 토픽에서 자세히 설명하고 있는데요.

. NET Matters - NamedGZipStream, Covariance and Contravariance
; https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/october/net-matters-namedgzipstream-covariance-and-contravariance

The GZipStream class implements the GZIP file format as defined in RFC 1952. The GZIP file format makes use of the DEFLATE compression algorithm, detailed in RFC 1951, and simply defines a header and footer that are used to encapsulate the compressed data, providing some metadata for it.

The problem you're running into is that the GZIP header format declares the name of the compressed file as optional.



따라서, 만약 파일 이름까지 포함하는 헤더를 지원하는 압축 데이터를 원한다면 위의 토픽에서 제공되는 NamedGZipStream을 사용하거나, 아니면 그에 준하는 기능을 지원하는 여타 공개된 라이브러리를 사용하셔야 합니다.

마지막으로, Zip에 대응되는 닷넷 기본 클래스는 존재하지 않습니다. 물론 고맙게도 다른 분들이 만들어 놓은 공개 라이브러리들이 있습니다.

DotNetZip - Zip file manipulation in .NET languages
; http://www.codeplex.com/DotNetZip

SharpZipLib
; http://www.icsharpcode.net/OpenSource/SharpZipLib/

그럼... 좀 정리가 된 것 같죠! ^^



[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/25/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 121  122  123  124  125  126  [127]  128  129  130  131  132  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
2913정성태4/30/201527612.NET Framework: 511. Build 2015 행사에서 소개된 (맥/리눅스/윈도우 용 무료) Visual Studio Code 개발 도구 [8]
2912정성태4/29/201523294오류 유형: 286. VirtualBox에 Windows 8/2012 설치 시 "Error Code: 0x000000C4" 오류 발생
2911정성태4/29/201521876오류 유형: 285. Visual Studio 2015를 제거한 경우 Microsoft.VisualStudio.Web.PageInspector.Loader 어셈블리를 못 찾는 문제 [2]
2910정성태4/29/201525744오류 유형: 284. System.TypeLoadException: Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly [1]
2909정성태4/29/201521901오류 유형: 283. WCF 연결 오류 - Expected record type 'PreambleAck'
2908정성태4/29/201530126오류 유형: 282. 원격에서 SQL 서버는 연결되지만, SQL Express는 연결되지 않는 경우
2907정성태4/29/201520324.NET Framework: 510. 제네릭(Generic) 인자에 대한 메타데이터 등록 확인
2906정성태4/28/201522843오류 유형: 281. DebugView로 인한 System.Diagnostics.Trace.WriteLine 멈춤(Hang) 현상
2905정성태4/27/201523360오류 유형: 280. HttpResponse.Headers.Add에서 "System.PlatformNotSupportedException: This operation requires IIS integrated pipeline mode." 예외 발생
2904정성태4/27/201528247DDK: 6. ZwTerminateProcess로 프로세스를 종료하는 Device Driver 프로그램 [2]파일 다운로드1
2903정성태4/20/201521902Windows: 110. (무료) 마이크로소프트 온라인 강좌 소개 - Azure에서 제공하는 계정 관리 서비스
2902정성태4/16/201527833Windows: 109. (무료) 마이크로소프트 온라인 강좌 소개 - Active Directory 이해
2901정성태4/15/201524446Windows: 108. (무료) 마이크로소프트 온라인 강좌 소개 - Windows Server 2012 R2 주요 기술 (Hyper-V 관점)
2900정성태3/24/201523572오류 유형: 279. robocopy 오류 - The file cannot be accessed by the system [4]
2899정성태3/24/201531641개발 환경 구성: 264. Visual Studio 2013 솔루션을 2015로 마이그레이션
2898정성태3/24/201522367개발 환경 구성: 263. SharePoint 2013을 Windows Server 2012 R2에 설치
2897정성태3/18/201520959오류 유형: 278. LoadLibrary("...") failed - Invalid access to memory location.
2896정성태3/18/201520975VC++: 90. Visual Studio 2013에서 Visual Basic 6용 ATL Control 제작
2895정성태3/18/201524233VC++: 89. Visual Studio 2015 - auto 반환 타입 및 thread_local 예약어 지원(C++ 11 표준) [2]
2894정성태3/18/201521995.NET Framework: 509. ELEMENT_TYPE_MODIFIER의 조합
2893정성태3/18/201522749오류 유형: 277. command line error MIDL1004: cannot execute C preprocessor cl.exe
2892정성태3/17/201527622오류 유형: 276. robocopy - Logon failure: unknown user name or bad password.
2891정성태3/17/201543664개발 환경 구성: 262. Visual Basic 6 (Enterprise Edition)을 Windows 7 x86에 설치하는 방법 [1]
2890정성태3/17/201525518오류 유형: 275. Internet Explorer - This page can't be displayed
2889정성태3/17/201526710Windows: 107. (2015-03-12) 업데이트 이후 작업 표시줄 또는 탐색기의 반응이 느려지는 문제 [1]
2888정성태3/17/201524535.NET Framework: 508. Visual Studio 빌드 - fatal error C1033: cannot open program database ''
... 121  122  123  124  125  126  [127]  128  129  130  131  132  133  134  135  ...