zipOpen 함수로 압축 파일을 생성하거나, 기존 압축 파일에 추가한다. 또는 다른 실행 파일에 압축한 데이터를 기록할 수 있다.(self extractor exe 생성할때 사용함.) zipClose 함수로 생성된 압축 파일을 닫아주어야 한다.
zipOpenNewFileInZip 함수로 압축 목록을 생성할 수 있다. 생성된 압축 목록에 zipWriteInFileInZip 함수로 데이터를 기록할 수 있다. zipCloseFileInZip 함수로 압축 목록을 닫아주어야 한다.
// zip_sample1.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <LibZ/zip.h>
#include <fstream>
#include <ctime>
int _tmain(int argc, _TCHAR* argv[])
{
do {
//zip 파일 생성
zipFile zf =zipOpen("c.zip",APPEND_STATUS_CREATE);
if( NULL == zf) break;
zip_fileinfo info;
memset(&info, 0, sizeof(zip_fileinfo) );
time_t nt;
time(&nt);
struct tm* tdata=localtime(&nt);
info.tmz_date.tm_hour = tdata->tm_hour;
info.tmz_date.tm_mday = tdata->tm_mday;
info.tmz_date.tm_min = tdata->tm_min;
info.tmz_date.tm_mon = tdata->tm_mon;
info.tmz_date.tm_sec = tdata->tm_sec;
info.tmz_date.tm_year = tdata->tm_year;
// zip 파일 목록 생성
int ret = zipOpenNewFileInZip(zf,"c.jpg",&info,NULL,0,NULL,0,"comment",
Z_DEFLATED,Z_DEFAULT_COMPRESSION);
///// 기록 시작...
do{
std::ifstream fp;
fp.open("c.jpg", std::ios_base::binary);
if( !fp ) break;
const int BUF=1024;
Bytef in[BUF];
do {
fp.read(( char*) in, BUF);
int readsize = (int)fp.gcount();
zipWriteInFileInZip(zf,(const void*) in, readsize);
} while( !fp.eof() );
fp.close();
} while(false);
//////기록 완료
zipCloseFileInZip(zf);//zip 목록 닫기
zipClose(zf,"global_comment");//zip 파일 닫기
}while(false);
return 0;
}