고정 Dictionary 기반 압축


LZ77 알고리즘은 search buffer, lookahead buffer로 구분하고, 뒤에 있는 버퍼가 앞에 있는 버퍼에서 매칭되는 부분을 축약하는 형태의 압축입니다. 결국 뒤 버퍼는 앞 버퍼를 마치 사전처럼 사용합니다. 따라서 LZ77 알고리즘을 Dictionary Compression Method를 사용하는 알고리즘이라고 할 수 있습니다.

만약 입력되는 데이터에 대한 정보를 알고 있다면 search buffer를 사용자가 제공하는 버퍼를 사용하도록 할 수 있습니다. 따라서 search buffer를 최소화하고, 성능이 빠른 압축이 가능해집니다.

deflateSetDictionary 함수와 inflateSetDictionary 함수를 사용해서 Dictionary Buffer를 지정할 수 있습니다.

동영상 강좌 예제 코딩
// Sample3_Dict.cpp : Defines the entry point for the console application.
//

#include  "stdafx.h"

#include  <LibZ/zlib.h>
#include  <iostream>

int  _tmain(int  argc, _TCHAR* argv[])
{
   const  Bytef dic[]="HELLO";
   const  int BUF = 4096;

   //압축시  데이터와  데이터  길이
   Bytef deflate_out[BUF];
   int deflate_size(0);

   //압축  해제  데이터와  데이터  길이
   Bytef inflate_out[BUF];
   int inflate_size(0);

   // 압축  계산.
   do{
      z_stream stream;
       stream.zalloc = Z_NULL;
       stream.zfree = Z_NULL;
       stream.opaque = Z_NULL;
      int ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
      if( Z_OK!=ret) break;

       deflateSetDictionary(&stream, dic, strlen( (const  char*)dic ) );

      Bytef in[] ="HELLOaHELLObHELLOaHELLObHELLOaHELLObHELLOaHELLObHELLOaHELLObHELLOaHELLOb";
       std::cout<<"Raw Data Size:"<<strlen( (const  char*)in)<<std::endl;

       stream.next_in = in;
       stream.avail_in = strlen( (const  char*)in);
      
       stream.next_out = deflate_out;
       stream.avail_out = BUF;

      do {
          deflate(&stream,Z_FINISH);
      } while( 0 != stream.avail_in);

       deflate_size = BUF - stream.avail_out;
       std::cout<< "Deflate Size:"<<deflate_size<<std::endl;

       deflateEnd(&stream);

   }while(false);


   /// 압축  해제
   do {
      z_stream stream;
       stream.zalloc = Z_NULL;
       stream.zfree = Z_NULL;
       stream.opaque = Z_NULL;
      int ret = inflateInit(&stream);
      if( Z_OK!=ret) break;

      //입력  데이터
       stream.next_in = deflate_out;
       stream.avail_in = deflate_size;

      //출력  버퍼
       stream.next_out = inflate_out;
       stream.avail_out = BUF;

      do {
          ret = inflate(&stream, Z_NO_FLUSH);
         if (Z_NEED_DICT == ret){
             inflateSetDictionary(&stream, dic, strlen( (const  char*) dic ) );
         }

      } while(0!=stream.avail_in);

       inflate_size = BUF - stream.avail_out;
       std::cout<<"Inflate size:"<<inflate_size<<std::endl;

       inflate_out[inflate_size]=NULL;
       std::cout<<"Inflate Data:"<<(const  char*) inflate_out<<std::endl;

       inflateEnd(&stream);
   }while(false);

    system("pause");

   return 0;
}

- 목록:

13 thoughts on “gz 파일 사용하기

  1. 안녕하세요 좋은 영상과 글을 잘 봤습니다.
    글에 보면 “입력 버퍼와 출력 버퍼를 고정으로한 압축 함수”에 대해서 어떤 함수를 봐야 하는지 알수 있을까요?
    좋은 글 감사드리며, 오래된 글에 문의를 남깁니다.
    감사합니다.

  2. 동영상 잘 보았습니다. 궁금한것이 있는데요, Z_SYNC_FLUSH 를 사용할 경우에, 만약 next_out buffer 크기가 압축한 data 를 담기에 충분하지 않는다면, 어떻게 해야 하는지요?

    • Z_SYNC_FLUSH는 쓰고 있는 비트를 모두 바이트 단위로 쓰라는 옵션임으로 일반적인 쓰기 과정에서 발생한 에러 코드를 반환하게 되어 있습니다. [Deflate 샘플] 동영상을 참고하세요.

  3. C로 압축하고 c#으로 압축을 푸는 방법이 있을까요?
    텍스트 파일을 작성시

    gzwritre(파일 포인터, 2019, sizeof(int));
    gzwritre(파일 포인터, 09, sizeof(int));
    gzwritre(파일 포인터, 17, sizeof(int));

    gzwritre(파일 포인터, 0.1, sizeof(float)); 결과값
    gzwritre(파일 포인터, 0.2, sizeof(float));
    gzwritre(파일 포인터, 0.3, sizeof(float));

    gzwritre(파일 포인터, 100.0, sizeof(float));

    이렇게 텍스트 파일을 저장(압축)했는데 C#에서 이 파일을 읽어서

    연 / 월 / 일 을 정수 변수에
    결과값을 실수배열에 저장하고 싶습니다.

  4. 분할 압축파일일 경우 zilb로 압축 해제가 가능 한가요?
    아니면 다른 라이브러리를 사용해야 할까요 ?

    • 별도로 지원하는 함수는 없지만 그냥 저장하는 과정에서 파일만 나눠 저장하는 것이 아닐지요.

  5. 안녕하세요
    c++에서 zlib uncompress로 .emf image 파일을 압축해제 하려고 합니다.
    그런데 손상된 파일이을 대상으로 진행할 경우 Z_DATA_ERROR을 반환하면서 자동종료됩니다.
    압축된 파일이 Z_DATA_ERROR를 반환하면(손상된 파일이면) 빈 .emf image 파일로 압축해제 되도록 하려면 어떻게 해야할까요?

  6. 리눅스에서 C언어를 사용해서 zlib으로 위와 같이 폴더(디렉토리)를 압축할수있나요?

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>