zlib 라이브러리는 다양한 형태의 압축 데이터를 생성할 수 있습니다. 대용량 데이터를 압축할 때는 do.. while 구문으로 반복적으로 압축하고 Z_NO_FLUSH 모드로 압축한 후 마지막에 Z_FINISH 모드로 압축합니다. 적은 메모리를 사용하는 데이터를 압축하는 경우, 즉 입력 버퍼가 몇 메가 안쪽이라고 100% 확신하거나, 충분히 예상 가능한 범위 내 버퍼를 사용한다면, 입력 버퍼와 출력 버퍼를 고정으로한 압축 함수를 사용할 수도 있습니다.
compress 함수는 입력 버퍼가 작은 경우, 한번에 압축 완료합니다. 즉 Z_FINISH 모드를 사용해서 압축합니다. 출력 버퍼는 입력 버퍼의 2배 + 13 바이트를 넘지 않습니다. compress 함수에 충분한 출력 버퍼를 제공하면 문제없이 압축이 되어집니다.
uncompress 함수는 compress 함수로 압축된 버퍼를 해제합니다. 고정 버퍼 Dictionary를 사용한 경우에는 해제되지 않습니다. 또한 Z_FINISH로 종료 시그널이 없는 데이터도 해제되지 않습니다. 결국 compress 함수로 압축된 데이터를 해제합니다. uncompress 함수로 해제된 데이터의 길이를 계산하는 함수는 없습니다. 결국 압축하는 프로그램과 압축 해제하는 프로그램은 최대 입력 버퍼의 길이, 최대 출력 버퍼의 길이를 미리 약속하고, 프로그래밍해야 합니다. 그렇지 않는 경우는 해당 함수를 사용하지 말고, zlib 기본 함수를 사용해서 프로그래밍해야 합니다.
// Simple6_simpcom.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <LibZ/zlib.h>
#include <string>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
const int BUF = 1024;
const int DBUF = BUF*2 +13;
Bytef raw_data[] ="안녕하세요.";
Bytef deflate_data[DBUF];
uLong raw_size = strlen( (const char*) raw_data);
uLong deflate_size = DBUF;
//compress 사용하기
{
compress(deflate_data,&deflate_size,raw_data, raw_size);
std::cout<<"Raw Data Size:"<< raw_size<<std::endl;
std::cout<<"Deflate Data Size:"<<deflate_size<<std::endl;
}
Bytef inflate_data[BUF];
uLong inflate_size = BUF;
//uncompress 사용하기
{
uncompress(inflate_data,&inflate_size,deflate_data, deflate_size);
std::cout<<"Deflate Data Size:"<<deflate_size<<std::endl;
std::cout<<"Inflate Size:"<< inflate_size<<std::endl;
inflate_data[inflate_size]=NULL;
std::cout<< "원본 데이터:"<< ( const char*)inflate_data<<std::endl;
}
system("pause");
return 0;
}