개발 / / 2024. 5. 19. 09:33

C++ zip 압축 파일 생성 라이브러리 ziplib 시작하기 (테스트 환경세팅, libzip, libzippp, zlib)

반응형

libzippp를 포기하고 zip 라이브러리를 사용할 다른 방안으로 ziplib 라이브러리를 테스트해보기로 하였다

 

 

시작하기

ziplib은 bitbucket 사이트에 documents를 준비해두었기 때문에 참고하여 세팅해보자

 

 

ziplib은 간단히 C++11 환경에서 ZIP 관련 작업을 할 수 있도록 만든 라이브러리이다

장점은 파일 관련해서 STL stream을 사용하기에 추가적으로 의존하는 다른 라이브러리가 크게 없다는 점

 

여러 특징들은 제처두고 example 사용법에 대해서 보자

 

example 프로젝트 실행하기

친절하게 step by step으로 프로젝트를 준비할 수 있게 설명이 되어있지는 않다

일단 github clone을 받자 

 

cmake를 안하더라도 solution 파일이 있다

 

일단 무턱대고 ZipLib 프로젝트를 빌드했는데 우선 빌드는 성공했다

 

실행을 해보면 이렇게 뜬다

즉 라이브러리 파일이 빌드되었다는 말이다. 이걸 사용해볼까 

우리는 release 플랫폼의 라이브러리가 필요하므로 release로 빌드시키자

 

release 버전 빌드가 되었고 안에 ZipLib, zlib 등 라이브러리가 있다

 

ZipLib이 의존하는 라이브러리

readme를 보면 ziplib은 zlib, lzma, bzip2를 포함하고 있다고 하니

해당 라이브러리들은 모두 필요할 것이다

새 프로젝트 생성

새 프로젝트를 생성해서 라이브러리를 넣고 세팅해보자

 

라이브러리 세팅

프로젝트에 외부 라이브러리 사용관련 세팅을 해준다

링크 일반과 입력에 라이브러리 경로와 넣을 라이브러리 파일 이름들을 종속성에 넣어주자

 

ZipLib의 기존 소스 코드 중 필요한 소스 파일들은 모두 프로젝트 복사해서 넣어주고

 

넣기로 세팅해놓은 라이브러리 파일들을 lib 폴더를 만들어서 넣어주면 된다

 

코드 작성

#include <iostream>
#include "ZipFile.h"
#include <cassert>
#include <algorithm>
#include <sstream>

#include <fstream>
using namespace std;

int main() {
	string filePath = "../test.zip";
	ZipArchive::Ptr archive = ZipFile::Open(filePath);

	ZipArchiveEntry::Ptr entry = archive->CreateEntry("content.txt");

	// if entry is nullptr, it means the file already exists in the archive
	assert(entry != nullptr);

	std::ifstream contentStream("content.txt", std::ios::binary); //should not be destroyed before archive is written
	entry->SetCompressionStream(contentStream);

	// you may of course save it somewhere else
	// note: the contentStream is pumped when the archive is saved
	// if you want to read the stream before saving of the archive,
	// it is need to set Immediate mode in SetCompressionStream method (see below)
	ZipFile::SaveAndClose(archive, filePath);


	// If you want to use std::ostream interface for custom purposes
	// such as sending data over network, where no seek() operation possible
	// or you want to remove unnecessary seek operations on HDD
	// mark entry to use data descriptor for pure streamed operation
	// see https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
	// entry->UseDataDescriptor(true); 
	// archive->WriteToStream(std::ostream);
}

bitbucket에 sample 코드가 만들어져 있는데 이를 참고로하여 테스트 코드를 작성하였다

 

해당 코드는 content.txt라는 파일을 담아 test.zip로 압축한 zip 파일을 생성한다

 

C++에서 zip 압축 파일로 export하는 작업이 필요하다면 해당 방법으로 시도해보면 될 것이다

반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유