7-ZIP32.DLLを使用しフォルダごと圧縮する


使いやすいように関数ポインタの型をtypedefする

typedef int (WINAPI *SevenZipFunc_)(const HWND _hwnd, LPCSTR _szCmdLine, LPSTR _szOutput, const DWORD _dwSize);

ボタンを押したら圧縮を開始
LGPLのため動的に呼び出すようにしている

void CMy7zipTestDlg::OnBnClickedBtnStart()
{
	USES_CONVERSION;

	HMODULE hModule = ::LoadLibrary(_T("7-zip32.dll"));
	
	if (hModule) {
		SevenZipFunc_ SevenZipFunc = (SevenZipFunc_)::GetProcAddress(hModule, "SevenZip");

		CString strFolderPath;
		GetDlgItemText(IDC_STATIC, strFolderPath);

		// ディレクトリ名の末尾は「\」で終わっている必要がある。
		// -hideオプションを外すと、状況表示のダイアログが表示される。
		LPSTR szCommand = new CHAR[MAX_PATH];
		sprintf_s(szCommand, MAX_PATH, "a -tzip C:\\test.zip %s\\ *", T2A(strFolderPath.GetBuffer(MAX_PATH)));
		strFolderPath.ReleaseBuffer();

		LPSTR lpszOutput = new CHAR[MAX_PATH * 4];
		::ZeroMemory(lpszOutput, MAX_PATH * 4);

		int result = SevenZipFunc(this->m_hWnd, szCommand, lpszOutput, MAX_PATH * 4);
		ASSERT(result == 0);

		delete  szCommand;
		delete  lpszOutput;

		::FreeLibrary(hModule);
		hModule = 0;
	}
}

7-ZIP32.DLL配布元
http://www.csdinc.co.jp/archiver/lib/7-zip32.html#abst

参照
http://hrgs.xrea.jp/2006/01/26/7_zip_sample_with_vc