티스토리 뷰

Visual Studio 2012 사용


기존 파일들을 Zip으로 압축하거나 압축해제 해보자.

파일 정보들은 Ini파일을 활용하여 저장할 계획이다.

참고 : http://zadd.tistory.com/52


새 MFC 윈폼 프로젝트를 만들어 ZipTest란 이름으로 프로젝트를 생성하였다.


1. Zip/Unzip 소스 및 헤더 추가


Zip.zip


이 소스를 활용하여 압축과 해제를 진행할 것이다.


받은 소스를 프로젝트에 복사해 넣고 아래와 같이 cpp 안에 zip.h와 unzip.h 헤더를 포함시킨다.

// ZipTestDlg.cpp : 구현 파일
//
#include "unzip.h"
#include "zip.h"

여기서 사용할 함수들을 살펴보자.


zip.h

HZIP CreateZip(const TCHAR *fn, const char *password);
ZRESULT ZipAdd(HZIP hz,const TCHAR *dstzn, const TCHAR *fn);
ZRESULT CloseZip(HZIP hz);

1) CreateZip: *fn 문자열로 Zip파일을 생성한다. *password에는 파일의 암호를 추가할 수 있다.

2) ZipAdd: CreateZip으로 생성한 Zip 파일에 *dstzn 문자열의 이름으로 *fn 문자열 위치에 있는 파일을 추가한다.

3) CloseZip: 생성한 Zip파일의 핸들을 닫는다.


unzip.h

HZIP OpenZip(const TCHAR *fn, const char *password);
ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn);
ZRESULT CloseZip(HZIP hz);

1) Open: *fn 문자열 경로에 있는 Zip 파일을 불러온다. *password에는 파일의 암호를 입력한다.

2) UnzipItem: 불러온 Zip 파일에서 index 숫자의 인덱스에 있는 파일을 *fn 문자열 경로로 압축해제한다.

3) CloseZip: Zip파일의 핸들을 닫는다.


2. 압축하기


윈폼에서 버튼을 만들어 버튼 클릭으로 특정 파일을 압축해보자.


void CZipTestDlg::OnBnClickedBtnZip()
{
	HZIP hz;
	ZRESULT zr;
	CString strPathZip = "D:\\ZIP\\test.zip";
	CString strPathIni = "D:\\test.ini";
	
	//D:\ZIP\ 경로에 test.zip이란 파일을 생성한다.
	CreateDirectory("D:\\ZIP", NULL);
	hz=CreateZip(strPathZip, 0);

	//zip파일 생성 실패시 처리
	if(hz==0) 
	{
		AfxMessageBox("Error: Failed to create Zip");
		return;
	}

	int i=0, nLen=0;
	CString strTmp="", strTmpPath="", strTmpName="";

	//INI 파일을 활용하여 압축할 파일 정보를 저장한다.
	//D드라이브에 test.ini란 파일이름으로 저장한다.
	//FTP란 섹션에 Total 키에는 파일 갯수, 그 이후부터는 순번대로 파일 이름을 넣는다.
	//INI파일은 D드라이브에 test.ini란 파일이름으로 저장한다.
	::WritePrivateProfileString("FTP","Total","1",strPathIni);
	::WritePrivateProfileString("FTP","1","D:\\test.txt",strPathIni);

	//ZipAdd 명령으로 D:\test.ini를 idx.ini로 바꾸어 D:\ZIP\test.zip 파일에 추가한다.
	zr=ZipAdd(hz,"idx.ini",strPathIni);	//압축할 파일 명"idx.ini"

	//ZipAdd 명령 실패시 처리	
	if(zr!=ZR_OK) 
	{
		AfxMessageBox("Error: Failed to add Zip");
		zr=CloseZip(hz); 
		return;
	}

	//ZipAdd 명령으로 다른 파일을 추가한다.
	{
		strTmpName="test.txt";			//Zip파일에 저장할 파일이름
		strTmpPath="D:\\test.txt";	//압축할 파일 위치
		zr=ZipAdd(hz, strTmpName, strTmpPath); 

		if(zr!=ZR_OK) 
		{
			AfxMessageBox("Error: Failed to add Zip");
			zr=CloseZip(hz); 
			return;
		}
	}

	//Zip닫기
	zr=CloseZip(hz); 

	if(zr!=ZR_OK)  
	{
		AfxMessageBox("Error: Failed to close zip");
		return;
	}
}

1) CreateZip() 함수를 사용하여 test.zip파일을 D:\ZIP 폴더에 생성한다. 폴더 생성은 CreateDirectory()함수를 사용했다.

2) test.ini 파일을 생성하여 압축할 파일 갯수와 파일관련 내용을 저장한 후, 생성한 test.zip파일에 추가한다.

3) 열린 ZIP관련 핸들을 닫는다.


2. 압축 해제하기


윈폼에서 버튼을 하나 만들어 버튼 클릭으로 test.zip을 압축해제 해보자.


void CZipTestDlg::OnBnClickedBtnUnzip()
{
	HZIP hz;
	ZRESULT zr;
	CString strPathZip = "D:\\ZIP\\test.zip";
	CString strPathIni = "D:\\ZIP\\test.ini";
	TCHAR strReadIni[200]={0};
	CString strTotal="";
	int nTotal=0, nLen=0, nPos=0, nRet=0;
	CString strTmp;

	//1
	hz=OpenZip(strPathZip,0);
	if(hz==0)
	{
		AfxMessageBox("Error: Failed to open Zip");
		return;
	}

	//2
	zr = UnzipItem(hz,0,strPathIni);

	if (zr!=ZR_OK) 
	{
		AfxMessageBox("UnZip Error");
		return;
	}

	Sleep(20);
	//3
	::GetPrivateProfileString("FTP", "Total", "0", strReadIni, 200, strPathIni);
	strTotal.Format("%s", strReadIni);
	nTotal=atoi(strTotal);
	::GetPrivateProfileString("FTP", "1", "", strReadIni, 200, strPathIni);
	strTmp.Format("%s",strReadIni);

	//4
	zr=UnzipItem(hz, 1, strTmp);

	if (zr!=ZR_OK) 
	{
		AfxMessageBox("UnZip Error2");
		zr=CloseZip(hz); 
		return;
	}

	//5
	zr=CloseZip(hz); 

	if(zr!=ZR_OK)  
	{
		AfxMessageBox("Close Zip Error");
		return;
	}
}

1) OpenZip() 함수를 사용하여 test.zip의 핸들을 얻는다.

2) UnzipItem() 함수를 사용하여 ini파일을 압축해제한다. 압축시 ini파일을 먼저 넣었기 때문에 인덱스번호에는 0을 입력한다.(인덱스는 0부터 시작함)

3) 압축해제한 Ini 파일로부터 키값들을 불러온다.("Total": 파일 갯수, "1": 첫번째 압축파일 원래 위치)

4) UnzipItem() 함수를 이용하여 Ini파일을 제외한 첫번째 파일인 D:\test.txt 파일을 압축해제한다. 압축해제할 위치는 INI파일에 "1" 키값에 저장된 경로를 활용하였다.

5) CloseZip() 함수를 사용하여 HZIP핸들을 닫는다.


댓글