⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipmemory.c

📁 是一个C语言的压缩和解压缩的DLL源程序.
💻 C
字号:
#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
ZipCreateBufferPtr		*lpZipCreateBuffer;
ZipAddBufferPtr			*lpZipAddBuffer;
ZipGetMemoryPtr			*lpZipGetMemory;
ZipClosePtr				*lpZipClose;
ZipFormatMessagePtr		*lpZipFormatMessage;

// Here is some data in memory which we'll compress into our zip file
const TCHAR Data1[] =	_T("This is some data.\r\n\r\nThis is line two.\r\n");
const TCHAR Data2[] =	_T("This is a test of LiteZip.dll.\r\n\r\nHopefully this worked!\r\n");





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

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

	msg[0] = 0;
	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &msg[0], 160, 0);
	MessageBox(0, &msg[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 5 functions in LiteZip.dll -- ZipCreateBuffer(), ZipAddBuffer()
		// ZipGetMemory(), ZipClose, and ZipFormatMessage
		lpZipCreateBuffer = (ZipCreateBufferPtr *)GetProcAddress(zipDll, ZIPCREATEBUFFERNAME);
		lpZipAddBuffer = (ZipAddBufferPtr *)GetProcAddress(zipDll, ZIPADDBUFFERNAME);
		lpZipGetMemory = (ZipGetMemoryPtr *)GetProcAddress(zipDll, ZIPGETMEMORYNAME);
		lpZipFormatMessage = (ZipFormatMessagePtr *)GetProcAddress(zipDll, ZIPFORMATMESSAGENAME);
		lpZipClose = (ZipClosePtr *)GetProcAddress(zipDll, ZIPCLOSENAME);

		// Create a ZIP archive in a memory buffer. Let LiteZip.dll allocate growable memory
		// from the memory pool. We'll set an upper limit of 100,000 bytes on this growable
		// memory.
		if (!(result = lpZipCreateBuffer(&hz, 0, 100000, 0)))
		{
			unsigned char	*buffer;
			DWORD			len;
			HANDLE			base;

			// Add the contents of the Data1[] memory buffer to the ZIP, and give it the
			// name "data1.txt".
			if ((result = lpZipAddBuffer(hz, _T("data1.txt"), &Data1[0], lstrlen(&Data1[0])))) goto bad2;

			// Add the contents of the Data2[] memory buffer to the ZIP, and give it the
			// name "data2.txt".
			if ((result = lpZipAddBuffer(hz, _T("data2.txt"), &Data2[0], lstrlen(&Data2[0]))))
			{
bad2:			lpZipClose(hz);
				goto bad;
			}

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

			// We're done adding files to our ZIP in memory, so let's get a pointer to that
			// memory, and the final size of it. NOTE: We tell LiteZip.dll to give us the
			// memory, and we'll free it when we're done.
			if ((result = lpZipGetMemory(hz, &buffer, &len, &base))) goto bad;

			// Let's write out the zip memory-buffer to a disk file named "test.zip"
			{
			HANDLE	handle;
			DWORD	written;
			
			handle = CreateFile(_T("test.zip"), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
			if (handle == INVALID_HANDLE_VALUE) show_errmsg();
			else
			{
				if (!WriteFile(handle, buffer, len, &written, 0)) show_errmsg();

				CloseHandle(handle);
			}
			}

			// Free the memory now that we're done with it.
			UnmapViewOfFile(buffer);
			CloseHandle(base);
		}
		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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -