inflate는 deflate로 압축된 데이터를 복원하는 알고리즘입니다.
deflate 함수와 동일하게 3개의 함수로 구성됩니다.
- inflateInit — 메모리 할당 및 내부 상태 변수 생성
- inflate –압축해제
- inflateEnd –메모리 해제
inflate는 deflate에서 사용한 옵션을 그대로 적용해주어야 정상적으로 압축 데이터를 복원할 수 있습니다. 만약 압축할 사용한 옵션을 사용하지 않는다면 정상적으로 파일을 복원할 수 없습니다.
동영상 강좌 샘플 코딩
// Sample1_Inflate.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <LibZ/zlib.h>
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream fp;
fp.open("test.enc", std::ios_base::binary);
if(!fp) return 0;
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
int ret = inflateInit(&stream);//메모리 할당
if( Z_OK != ret){
fp.close();
return 0;
}
const int BUF=1024;
Bytef in[BUF];
Bytef out[BUF];
std::ofstream wp;
wp.open("test_inflate.bmp",std::ios_base::binary);
do {
fp.read( (char*) in, BUF);
int readsize =(int) fp.gcount();
//입력 버퍼 설정
stream.avail_in = readsize;
stream.next_in = in;
do {// 모든 입력 버퍼 연산 실행
stream.avail_out = BUF;
stream.next_out = out;
ret = inflate(&stream,Z_NO_FLUSH);
//실행된 결과 저장
int dsize = BUF - stream.avail_out;
wp.write( (const char*) out, dsize);
} while( 0==stream.avail_out);
} while(Z_STREAM_END != ret || !fp.eof() );
wp.close();
inflateEnd(&stream);//메모리 해제
fp.close();
return 0;
}