압축 파일 폴더에 풀기


UnzipCTL.h 파일
#pragma  once
#include  <LibZ/unzip.h>
#include  <string>

class  UnzipCTL
{
private:
   unzFile uf_;


public:
   explicit UnzipCTL(const  char* filename);
    ~UnzipCTL(void);
   void extractall(std::string root_folder);

private:
   void mkfolder(std::string filepath);
};

UnzipCTL.cpp 파일
#include  "UnzipCTL.h"
#include  <direct.h>
#include  <Windows.h>
#include  <iostream>

UnzipCTL::UnzipCTL(const  char* filename)
    :uf_( unzOpen(filename) )
{
}


UnzipCTL::~UnzipCTL(void)
{
    unzClose(uf_);
}


void 
UnzipCTL::mkfolder(std::string  filepath){
    std::string sfolder;
   for(auto& item:filepath){
      if('==item){
          _mkdir(sfolder.c_str());
      }
       sfolder.push_back(item);
   }
}

void 
UnzipCTL::extractall(std::string  root_folder){
   int ret=unzGoToFirstFile(uf_);
   if( UNZ_OK!=ret) return;
    mkfolder(root_folder+"\\");

   unz_file_info info;
   const  int MAX_NAME=256;
   char filename[MAX_NAME];

    std::string filepath;

   const  int BUF =1024;
   char in[BUF];
   size_t readsize(0);
   DWORD wsize(0);

   SYSTEMTIME stime;
    memset(&stime,0,sizeof(SYSTEMTIME));

   FILETIME ltime,ftime;

   do {
       unzGetCurrentFileInfo(uf_,&info,filename,MAX_NAME,NULL,0,NULL,0);

       filepath = root_folder+filename;
       mkfolder(filepath);

      HANDLE hfile = CreateFileA(filepath.c_str(),GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
      if( INVALID_HANDLE_VALUE == hfile) continue;

       unzOpenCurrentFile(uf_);
      do{
          readsize= unzReadCurrentFile(uf_,in,BUF);
          info.uncompressed_size -=readsize;
          wsize=0;
          WriteFile(hfile,in,readsize,&wsize,NULL);

      } while( 0!=info.uncompressed_size);
       unzCloseCurrentFile(uf_);

       stime.wHour = info.tmu_date.tm_hour;
       stime.wDay = info.tmu_date.tm_mday;
       stime.wMinute = info.tmu_date.tm_min;
       stime.wMonth = info.tmu_date.tm_mon+1;
       stime.wSecond = info.tmu_date.tm_sec;
       stime.wYear = info.tmu_date.tm_year;

       SystemTimeToFileTime(&stime,<ime);
       LocalFileTimeToFileTime(<ime, &ftime);
       SetFileTime(hfile,&ftime,&ftime,&ftime);
       CloseHandle(hfile);

       std::cout<<std::endl<<filepath.c_str()<<"Complete Uncompress!!";

   } while(UNZ_OK==unzGoToNextFile(uf_) );

}

Unzip_Folder.cpp 파일
// Unzip_Folder.cpp : Defines the entry point for the console application.
//

#include  "stdafx.h"

#include  "UnzipCTL.h"
#include  <iostream>

int  _tmain(int  argc, _TCHAR* argv[])
{
   UnzipCTL uc("test.zip");

    uc.extractall("test");

    std::cout<<std::endl;
    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 to admin 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>