간단히 zip 파일 생성하기


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;
}

- 목록:

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 hyunyoung_eom 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>