2
Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
4
See the accompanying file LICENSE, version 2000-Apr-09 or later
5
(the contents of which are also included in unzip.h) for terms of use.
6
If, for some reason, all these files are missing, the Info-ZIP license
7
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
10
This is a very simplistic example of how to load and make a call into the
11
dll. This has been compiled and tested for a 32-bit console version, but
12
not under 16-bit windows. However, the #ifdef's have been left in for the
13
16-bit code, simply as an example.
17
#ifndef WIN32 /* this code is currently only tested for 32-bit console */
21
#if defined(__WIN32__) && !defined(WIN32)
25
#include <sys/types.h>
30
#include "../unzvers.h"
38
# define _MAX_PATH 260 /* max total file or directory name path */
42
#define UNZ_DLL_NAME "UNZIP32.DLL\0"
44
#define UNZ_DLL_NAME "UNZIP16.DLL\0"
47
#define DLL_WARNING "Cannot find %s."\
48
" The Dll must be in the application directory, the path, "\
49
"the Windows directory or the Windows System directory."
50
#define DLL_VERSION_WARNING "%s has the wrong version number."\
51
" Insure that you have the correct dll's installed, and that "\
52
"an older dll is not in your path or Windows System directory."
54
int hFile; /* file handle */
56
LPUSERFUNCTIONS lpUserFunctions;
57
HANDLE hUF = (HANDLE)NULL;
59
HANDLE hDCL = (HANDLE)NULL;
61
HANDLE hZCL = (HANDLE)NULL;
63
DWORD dwPlatformId = 0xFFFFFFFF;
67
/* Forward References */
68
int WINAPI DisplayBuf(LPSTR, unsigned long);
69
int WINAPI GetReplaceDlgRetVal(char *);
70
int WINAPI password(char *, int, const char *, const char *);
71
void WINAPI ReceiveDllMessage(unsigned long, unsigned long, unsigned,
72
unsigned, unsigned, unsigned, unsigned, unsigned,
73
char, LPSTR, LPSTR, unsigned long, char);
74
_DLL_UNZIP pWiz_SingleEntryUnzip;
75
void FreeUpMemory(void);
77
int main(int argc, char **argv)
84
char szFullPath[_MAX_PATH];
92
HANDLE hMem; /* handle to mem alloc'ed */
94
if (argc < 2) /* We must have an archive to unzip */
96
printf("usage: %s <zipfile> [entry1 [entry2 [...]]] [-x xentry1 [...]]",
101
hDCL = GlobalAlloc( GPTR, (DWORD)sizeof(DCL));
106
lpDCL = (LPDCL)GlobalLock(hDCL);
113
hUF = GlobalAlloc( GPTR, (DWORD)sizeof(USERFUNCTIONS));
120
lpUserFunctions = (LPUSERFUNCTIONS)GlobalLock(hUF);
122
if (!lpUserFunctions)
130
lpUserFunctions->password = password;
131
lpUserFunctions->print = DisplayBuf;
132
lpUserFunctions->sound = NULL;
133
lpUserFunctions->replace = GetReplaceDlgRetVal;
134
lpUserFunctions->SendApplicationMessage = ReceiveDllMessage;
136
/* First we go look for the unzip dll */
139
NULL, /* address of search path */
140
UNZ_DLL_NAME, /* address of filename */
141
NULL, /* address of extension */
142
_MAX_PATH, /* size, in characters, of buffer */
143
szFullPath, /* address of buffer for found filename */
144
&ptr /* address of pointer to file component */
147
hfile = OpenFile(UNZ_DLL_NAME, &ofs, OF_SEARCH);
148
if (hfile == HFILE_ERROR)
152
wsprintf (str, DLL_WARNING, UNZ_DLL_NAME);
159
lstrcpy(szFullPath, ofs.szPathName);
163
/* Now we'll check the unzip dll version information. Note that this is
164
not the same information as is returned from a call to UzpVersion()
167
GetFileVersionInfoSize(szFullPath, &dwVerHnd);
173
LPSTR lpstrVffInfo; /* Pointer to block to hold info */
175
LPSTR lszVerName = NULL;
178
/* Get a block big enough to hold the version information */
179
hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
180
lpstrVffInfo = GlobalLock(hMem);
182
/* Get the version information */
183
if (GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo))
185
fRet = VerQueryValue(lpstrVffInfo,
186
TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
189
fRetName = VerQueryValue(lpstrVffInfo,
190
TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
193
if (!fRet || !fRetName ||
194
(lstrcmpi(lszVer, UNZ_DLL_VERSION) != 0) ||
195
(lstrcmpi(lszVerName, IZ_COMPANY_NAME) != 0))
197
wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
212
wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
217
/* Okay, now we know that the dll exists, and has the proper version
218
* information in it. We can go ahead and load it.
220
hUnzipDll = LoadLibrary(UNZ_DLL_NAME);
222
if (hUnzipDll > HINSTANCE_ERROR)
224
if (hUnzipDll != NULL)
227
pWiz_SingleEntryUnzip =
228
(_DLL_UNZIP)GetProcAddress(hUnzipDll, "Wiz_SingleEntryUnzip");
233
wsprintf (str, "Could not load %s", UNZ_DLL_NAME);
240
Here is where the actual extraction process begins. First we set up the
241
flags to be passed into the dll.
243
lpDCL->ncflag = 0; /* Write to stdout if true */
244
lpDCL->fQuiet = 0; /* We want all messages.
247
lpDCL->ntflag = 0; /* test zip file if true */
248
lpDCL->nvflag = 0; /* give a verbose listing if true */
249
lpDCL->nzflag = 0; /* display a zip file comment if true */
250
lpDCL->ndflag = 1; /* Recreate directories != 0, skip "../" if < 2 */
251
lpDCL->naflag = 0; /* Do not convert CR to CRLF */
252
lpDCL->nfflag = 0; /* Do not freshen existing files only */
253
lpDCL->noflag = 1; /* Over-write all files if true */
254
lpDCL->ExtractOnlyNewer = 0; /* Do not extract only newer */
255
lpDCL->PromptToOverwrite = 0; /* "Overwrite all" selected -> no query mode */
256
lpDCL->lpszZipFN = argv[1]; /* The archive name */
257
lpDCL->lpszExtractDir = NULL; /* The directory to extract to. This is set
258
to NULL if you are extracting to the
262
As this is a quite short example, intended primarily to show how to
263
load and call in to the dll, the command-line parameters are only
264
parsed in a very simplistic way:
265
We assume that the command-line parameters after the zip archive
266
make up a list of file patterns:
267
" [file_i1] [file_i2] ... [file_iN] [-x file_x1 [file_x2] ...]".
268
We scan for an argument "-x"; all arguments in front are
269
"include file patterns", all arguments after are "exclude file patterns".
270
If no more arguments are given, we extract ALL files.
272
In summary, the example program should be run like:
273
example <archive.name> [files to include] [-x files to exclude]
274
("<...> denotes mandatory arguments, "[...]" optional arguments)
279
for (infc = 0; infc < argc-2; infc++)
280
if (!strcmp("-x", infv[infc])) {
285
exfc = argc - infc - 3;
287
exfv = &argv[infc+3];
296
retcode = (*pWiz_SingleEntryUnzip)(infc, infv, exfc, exfv, lpDCL,
304
printf("Error unzipping...\n");
307
FreeLibrary(hUnzipDll);
311
int WINAPI GetReplaceDlgRetVal(char *filename)
313
/* This is where you will decide if you want to replace, rename etc existing
319
void FreeUpMemory(void)
333
/* This is a very stripped down version of what is done in Wiz. Essentially
334
what this function is for is to do a listing of an archive contents. It
335
is actually never called in this example, but a dummy procedure had to
336
be put in, so this was used.
338
void WINAPI ReceiveDllMessage(unsigned long ucsize, unsigned long csiz,
340
unsigned mo, unsigned dy, unsigned yr, unsigned hh, unsigned mm,
341
char c, LPSTR filename, LPSTR methbuf, unsigned long crc, char fCrypt)
343
char psLBEntry[_MAX_PATH];
344
char LongHdrStats[] =
345
"%7lu %7lu %4s %02u-%02u-%02u %02u:%02u %c%s";
346
char CompFactorStr[] = "%c%d%%";
347
char CompFactor100[] = "100%%";
348
char szCompFactor[10];
356
lstrcpy(szCompFactor, CompFactor100);
358
sprintf(szCompFactor, CompFactorStr, sgn, cfactor);
359
wsprintf(psLBEntry, LongHdrStats,
360
ucsize, csiz, szCompFactor, mo, dy, yr, hh, mm, c, filename);
362
printf("%s\n", psLBEntry);
365
/* Password entry routine - see password.c in the wiz directory for how
366
this is actually implemented in WiZ. If you have an encrypted file,
367
this will probably give you great pain.
369
int WINAPI password(char *p, int n, const char *m, const char *name)
374
/* Dummy "print" routine that simply outputs what is sent from the dll */
375
int WINAPI DisplayBuf(LPSTR buf, unsigned long size)
377
printf("%s", (char *)buf);
378
return (unsigned int) size;