간단히 zip 파일 해제하기


unzOpen 함수로 zip 파일을 열수 있습니다. 열린 zip 파일은 unzClose 함수로 닫아주어야 합니다.

unzGoToFirstFile 함수와 unzGoToNextFile 함수로 순차적으로 파일 목록에 접근할 수 있습니다.

현재 방문중인 파일 정보는 unzGetCurrentFileInfo 함수로 확인할 수 있습니다.
현재 방문중인 파일은 unzOpenCurrentFile 함수로 오픈할 수 있습니다.
오픈된 파일 목록은 unzReadCurrentFile 함수로 데이터를 읽어낼 수 있습니다.
오픈된 파일 목록은 unzCloseCurrentFile 함수로 닫아주어야 합니다.

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

#include  "stdafx.h"

#include  <LibZ/unzip.h>
#include  <fstream>
#include  <iostream>

int  _tmain(int  argc, _TCHAR* argv[])
{
   do {
      unzFile uf = unzOpen("c.zip");
      if(NULL == uf) break;

      int ret = unzGoToFirstFile(uf);
      if( UNZ_OK != ret){
          unzClose(uf);
         break;
      }

      /// 현재  방문중인  파일  목록  정보  추출  
      const  int MAX_PATH = 256;
      const  int MAX_COMMENT = 256;

      char filename[MAX_PATH];      
      char comment[MAX_COMMENT];

      unz_file_info info;
       unzGetCurrentFileInfo(uf,&info,filename, MAX_PATH, NULL, 0, comment, MAX_COMMENT);

       std::cout<<"filename:"<< filename <<" Comment:"<<comment<<std::endl;
       std::cout<<" compressed_size:"<< info.compressed_size<<" uncompressed_size:"<< info.uncompressed_size <<std::endl;


      //현재  파일을  열기.
       ret = unzOpenCurrentFile(uf);

      const  int BUF =1024;
      Bytef in[BUF];
      int readsize(0);

       std::ofstream op;
       op.open(filename, std::ios_base::binary);

      do {
          readsize = unzReadCurrentFile(uf,(void*) in, BUF);
          op.write( (const  char*) in, readsize);

      } while( 0 != readsize);

       op.close();

       unzCloseCurrentFile(uf);   

      /////
       unzClose(uf);

   } while(false);

    system("pause");

   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

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>