zipdir.c

来自「是一个C语言的压缩和解压缩的DLL源程序.」· C语言 代码 · 共 100 行

C
100
字号
#include <windows.h>
#include <tchar.h>

// Include LiteZip.h for creating a ZIP archive
#include "../LiteZip.h"





// Where we store the pointers to LiteZip.dll's functions that we call
ZipCreateFilePtr		*lpZipCreateFile;
ZipAddDirPtr			*lpZipAddDir;
ZipClosePtr				*lpZipClose;
ZipFormatMessagePtr		*lpZipFormatMessage;





/*********************** show_errmsg() **********************
 * Displays a messagebox for the passed OS error number.
 */

void show_errmsg(void)
{
	TCHAR buffer[160];

	buffer[0] = 0;
	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buffer[0], 160, 0);
	MessageBox(0, &buffer[0], _T("Error"), MB_OK);
}





/************************** main() *************************
 * Program entry point.
 */

int main()
{
	HMODULE		zipDll;
	HZIP		hz;
	DWORD		result;

	// Open the LiteZip.DLL. Note: If LiteZip.dll is not placed in a path that can be found
	// by this app, then LoadLibrary will fail. So, either copy LiteZip.dll to the same
	// directory as this EXE, or to some directory that Windows is set to search.
	if ((zipDll = (HMODULE)LoadLibrary(_T("../LiteZip.dll"))))
	{
		// Get the addresses of 4 functions in LiteZip.dll -- ZipCreateFile(), ZipAddDir()
		// ZipClose(), and ZipFormatMessage.
		lpZipCreateFile = (ZipCreateFilePtr *)GetProcAddress(zipDll, ZIPCREATEFILENAME);
		lpZipAddDir = (ZipAddDirPtr *)GetProcAddress(zipDll, ZIPADDDIRNAME);
		lpZipClose = (ZipClosePtr *)GetProcAddress(zipDll, ZIPCLOSENAME);
		lpZipFormatMessage = (ZipFormatMessagePtr *)GetProcAddress(zipDll, ZIPFORMATMESSAGENAME);

		// Create a ZIP archive on disk named "test.zip".
		if (!(result = lpZipCreateFile(&hz, _T("test.zip"), 0)))
		{
			TCHAR		buffer[MAX_PATH];

			result = GetCurrentDirectory(MAX_PATH, &buffer[0]);
			lstrcpy(&buffer[result], "\\MyDir");

			// Add the contents of the dir "MyDir" (in the current dir) to the ZIP, and give the
			// names a relative dir off of the current dir
			if ((result = lpZipAddDir(hz, &buffer[0], result + 1)))
			{
				lpZipClose(hz);
				goto bad;
			}

			// Here we could call ZipAddFile to add more files to the ZIP archive, or ZipAddDir
			// to add more directories. We could also call ZipAddBuffer to add the contents of
			// some memory buffer as a "file" inside the zip. Or, we could call ZipAddHandle to
			// add the contents of some open file or pipe.

			// Done adding files, so close the ZIP archive.
			lpZipClose(hz);
		}
		else
		{
			TCHAR	msg[100];

bad:		lpZipFormatMessage(result, msg, sizeof(msg));
			MessageBox(0, &msg[0], _T("Error"), MB_OK);
		}

		// Free the LiteZip.DLL
		FreeLibrary(zipDll);
	}
	else
		show_errmsg();

	return(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?