zlib 압축

|

zlib를 이용한 zip 압축 기능 구현 테스트 프로그램.

단순 압축 기능만 콘솔 프로그램으로 구현해본 것입니다.

 

소스는 다음과 같습니다.

 

/*

 파일명 : ZipTest.cpp

 사용법 : ZipTest [filename]

 압축하고자 하는 filename 입력하면 filename.zip이라는 압축파일이

 생성된다. 

 */

 

// 표준 C헤더파일

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

// zlib 헤더파일 

#include <zlib.h>

 

int main(int argc, char *argv[])

{

char    *filename   = NULL;

char    *gzfilename = NULL;

gzFile  zf;

int     n;

char    buf[256];

int     lerrno;

             FILE *fp;

 

if(argc !=2) {

             printf("Usage : ZipTest [file name]\n");

             exit(0);

}

filename = argv[1];

 

// 압축파일의 이름은 filename.zip 으로 한다.

gzfilename = (char *)malloc(strlen(filename)*sizeof(char));

sprintf(gzfilename, "%s.zip", filename);

 

if ((fp = fopen(filename, "rb")) == NULL) {

             printf("file open error\n");

             exit(0);   

}

 

// 압축파일을 연다.

if ((zf = gzopen(gzfilename, "wb")) == NULL) {

             exit(0);

}

 

// 원본파일을 에서 데이타를 읽어들이고

// gzwrite함수를 이용해서 데이터를 압축하고 파일에 쓴다.  

while((n = fread(buf, sizeof(char), 255, fp)) > 0)

{

if (gzwrite(zf, buf, n) < 0) {

                          printf("%s\n",gzerror(zf, &lerrno));

                          exit(0);

             }

}

 

gzclose(zf);

 

printf("compression completed : %s => %s\n", filename, gzfilename);

 

return 0;

}

앞서 만든 ZipTest 만든 압축 프로그램을 풀어주는 UnzipTest 입니다.

역시 기능 구현 테스트만을 위해 간단히 콘솔 프로그램으로 만들었고 zlib 이용했습니다..

소스는 다음과 같습니다.

 

// UnzipTest.cpp : Defines the entry point for the console application.

 

#include <stdio.h>

#include <stdlib.h>

#include <zlib.h>

 

#define BUF_SIZE 1024

 

int main(int argc, char* argv[])

{

             char *srcFileName, *destFileName;

             gzFile zfSrc;

             FILE *fpDest;

             char buf[BUF_SIZE];

             int gzr;

 

             // 파라미터가 맞지 않으면 종료

             if(argc != 3) {

                           printf("Usage: UnzipTest [source filename] [destination filename]");

                          exit(0);

             }

 

             srcFileName = argv[1];

             destFileName = argv[2];

 

             // 파일이 존재하지 않으면 종료

             if((zfSrc = gzopen(srcFileName, "rb")) == NULL) {

                           printf("can't open source file");

                           exit(0);

             }

 

             // destination 파일 열기

             if((fpDest = fopen(destFileName, "wb")) == NULL) {

                           gzclose(zfSrc);

                           printf("can't create destination file");

                           exit(0);

             }

 

             // 압축 풀기

             while((gzr = gzread(zfSrc, buf, BUF_SIZE)) > 0) {

                           fwrite(buf, sizeof(char), gzr, fpDest);

             }

 

             gzclose(zfSrc);

             fclose(fpDest);

 

             // 에러발생시 종료

             if(gzr < 0) {

                           printf("Error occured.");

                           exit(0);

             }

 

             printf("Decompression completed.");

             return 0;

}

 

 

 

일단 기본적인 압축  압축 푸는 기능은 구현할  있게 되었습니다하지만 여러 파일(폴더 포함) 하나의 압축파일로 묶어서 압축하는  어떻게 해야 할지 아직까지는 모르겠군요좀더 연구를 해보야야겠습니다.

 

앞서 만든 두개의 ZipTest, UnzipTest 프로그램은 zip 압축 프로그램이 아니라 gz 압축 프로그램이었다. 첨엔 zlib가 zip 압축을 위한 라이브러리인줄 알았으나 사실은 gz 압축용 라이브러리였던 것이다. 이런..

(혹시 믿고 따라하신 분이 계셨다면 정말로 사과의 말씀드립니다. 사실 저도 몰랐답니다. ㅠㅠ)

 

그렇다면 zlib는 zip 압축을 지원하지 않는가? http://zlib.net 의 FAQ에서는 다음과 같이 대답해주고 있다.

 

Can zlib handle .zip archives?

 

Not by itself, no. See the directory contrib/minizip in the zlib distribution.

 

Not by itself 라니, 이 무슨 애매모호한 대답인가. -_-; 하지만 이 대답과 기타 여러 정황(?)으로 볼때 zip과 gz은 전혀 상관 없는 것도 아니라는 것이다. http://www.winimage.com/zLibDll/unzip.html 를 보면 이런 말이 나온다.

 

Using Unzip package
The Unzip source is only two file : unzip.h and unzip.c. But it use the Zlib files.

miniunz.c is a very simple, but real unzip program (display file in zipfile or extract them).

 

Using Zip package
The Zip source is only two file : zip.h and zip.c. But it use the Zlib files.

minizip.c is a very simple, but real zip program.

 

zip과 unzip 기능을 해주는 소스는 내부적으로 zlib를 이용한다고 한다. 이런 추리가 가능하다. gz은 단순 파일 압축이며, zip은 zlib 등의 gz 압축 알고리즘을 이용하되 이를 다수의 파일 및 폴더에 대한 압축이 가능하도록 만든 것이다라는.. 물론 틀린 추론일 수도 있다. -.-;

 

하지만 분명한 것은 Gilles Vollant가 만든 minizip의 네개의 소스 zip.h, zip.c, unzip.h, unzip.c를 이용하면 zip 압축 기능의 구현이 가능할 것이라는 사실이다.

 

헌데 여기서 한가지 문제가 있다. zip.c, unzip.c 라는 파일명을 보아도 알수 있지만 이것은 C++이 아닌 C로 쓰여진 프로그램이라는 것이다. 물론 VC++에서 C 컴파일이 가능하지만, 이를 C++로 래핑(wrapping)해 놓은 소스가 있다.

'개발/활용정보 > C, C++, C#' 카테고리의 다른 글

dynamic programming  (0) 2013.09.28
C versus C++  (1) 2011.05.31
And