summaryrefslogtreecommitdiff
path: root/plugins/Db_autobackups/src/zip.cpp
blob: 2b30fa7430de3dc4ddfd2c7d1f7975fd91162b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdafx.h"
#include "..\..\libs\zlib\src\zip.h"

int CreateZipFile(const char *szDestPath, OBJLIST<ZipFile> &lstFiles, const std::function<bool(size_t)> &fnCallback)
{
	zipFile hZip = zipOpen2_64(_A2T(szDestPath), APPEND_STATUS_CREATE, NULL, NULL);
	if (!hZip) return 1;

	zip_fileinfo fi = { 0 };

	for (int i = 0; i < lstFiles.getCount(); i++)
	{
		ZipFile &zf = lstFiles[i];

		HANDLE hSrcFile = CreateFileA(zf.sPath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		int iOpenRes = zipOpenNewFileInZip(hZip, zf.sZipPath.c_str(), &fi, NULL, 0, NULL, 0, "", Z_DEFLATED, Z_BEST_COMPRESSION);

		if (hSrcFile != INVALID_HANDLE_VALUE && iOpenRes == ZIP_OK)
		{
			DWORD dwRead;
			BYTE buf[0x40000];

			while (ReadFile(hSrcFile, buf, sizeof(buf), &dwRead, nullptr) && dwRead && (zipWriteInFileInZip(hZip, buf, dwRead) == ZIP_OK));
			zipCloseFileInZip(hZip);
			CloseHandle(hSrcFile);

			if (!fnCallback(i))
				return 3;
		}
	}

	zipClose(hZip, CMStringA(FORMAT, Translate("Miranda NG [%s] Database Backup"), g_szMirVer));
	return 0;
}