📄 zipfile.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
ZipCreateFilePtr *lpZipCreateFile;
ZipAddFilePtr *lpZipAddFile;
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(), ZipAddFile()
// ZipClose(), and ZipFormatMessage.
lpZipCreateFile = (ZipCreateFilePtr *)GetProcAddress(zipDll, ZIPCREATEFILENAME);
lpZipAddFile = (ZipAddFilePtr *)GetProcAddress(zipDll, ZIPADDFILENAME);
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)))
{
// Add the file "test.jpg" to the ZIP, and give it the same name inside the ZIP.
if ((result = lpZipAddFile(hz, _T("test.jpg"), _T("test.jpg"))) ||
// Add the file "Readme.txt" to the ZIP, and give it the same name inside the ZIP.
(result = lpZipAddFile(hz, _T("Readme.txt"), _T("Readme.txt"))))
{
lpZipClose(hz);
goto bad;
}
// Here we could call ZipAddFile to add more files to the ZIP archive. 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -