Microsoft MVP성태의 닷넷 이야기
.NET Framework: 135. C# - Deflate, GZip, Zip [링크 복사], [링크+제목 복사],
조회: 33342
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 46  [47]  48  49  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12765정성태8/9/202114279Java: 31. Cannot load JDBC driver class 'org.mysql.jdbc.Driver'
12764정성태8/9/202152509Java: 30. XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid
12763정성태8/9/202116256Java: 29. java.lang.NullPointerException - com.mysql.jdbc.ConnectionImpl.getServerCharset
12762정성태8/8/202119539Java: 28. IntelliJ - Unable to open debugger port 오류
12761정성태8/8/202116216Java: 27. IntelliJ - java: package javax.inject does not exist [2]
12760정성태8/8/202112992개발 환경 구성: 594. 전용 "Command Prompt for ..." 단축 아이콘 만들기
12759정성태8/8/202117635Java: 26. IntelliJ + Spring Framework + 새로운 Controller 추가 [2]파일 다운로드1
12758정성태8/7/202116985오류 유형: 751. Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
12757정성태8/7/202117660Java: 25. IntelliJ + Spring Framework 프로젝트 생성
12756정성태8/6/202115839.NET Framework: 1084. C# - .NET Core Web API 단위 테스트 방법 [1]파일 다운로드1
12755정성태8/5/202115958개발 환경 구성: 593. MSTest - 단위 테스트에 static/instance 유형의 private 멤버 접근 방법파일 다운로드1
12754정성태8/5/202116385오류 유형: 750. manage.py - Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
12753정성태8/5/202117317오류 유형: 749. PyCharm - Error: Django is not importable in this environment
12752정성태8/4/202114190개발 환경 구성: 592. JetBrains의 IDE(예를 들어, PyCharm)에서 Visual Studio 키보드 매핑 적용
12751정성태8/4/202117045개발 환경 구성: 591. Windows 10 WSL2 환경에서 docker-compose 빌드하는 방법
12750정성태8/3/202114076디버깅 기술: 181. windbg - 콜 스택의 "Call Site" 오프셋 값이 가리키는 위치
12749정성태8/2/202113599개발 환경 구성: 590. Visual Studio 2017부터 단위 테스트에 DataRow 특성 지원
12748정성태8/2/202114613개발 환경 구성: 589. Azure Active Directory - tenant의 관리자(admin) 계정 로그인 방법
12747정성태8/1/202114851오류 유형: 748. 오류 기록 - MICROSOFT GRAPH – HOW TO IMPLEMENT IAUTHENTICATIONPROVIDER파일 다운로드1
12746정성태7/31/202119513개발 환경 구성: 588. 네트워크 장비 환경을 시뮬레이션하는 Packet Tracer 프로그램 소개
12745정성태7/31/202115187개발 환경 구성: 587. Azure Active Directory - tenant의 관리자 계정 로그인 방법
12744정성태7/30/202115481개발 환경 구성: 586. Azure Active Directory에 연결된 App 목록을 확인하는 방법?
12743정성태7/30/202116716.NET Framework: 1083. Azure Active Directory - 외부 Token Cache 저장소를 사용하는 방법파일 다운로드1
12742정성태7/30/202114686개발 환경 구성: 585. Azure AD 인증을 위한 사용자 인증 유형
12741정성태7/29/202116269.NET Framework: 1082. Azure Active Directory - Microsoft Graph API 호출 방법파일 다운로드1
12740정성태7/29/202114784오류 유형: 747. SharePoint - InvalidOperationException 0x80131509
... 46  [47]  48  49  50  51  52  53  54  55  56  57  58  59  60  ...