1/*
2  Copyright (c) 1990-1999 Info-ZIP.  All rights reserved.
3
4  See the accompanying file LICENSE, version 1999-Oct-05 or later
5  (the contents of which are also included in zip.h) for terms of use.
6  If, for some reason, both of these files are missing, the Info-ZIP license
7  also may be found at:  ftp://ftp.cdrom.com/pub/infozip/license.html
8*/
9/*
10 A very simplistic example of how to load the zip dll and make a call into it.
11 Note that none of the command line options are implemented in this example.
12
13 */
14
15#ifndef WIN32
16#  define WIN32
17#endif
18#define API
19
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <time.h>
23#include <string.h>
24#ifdef __BORLANDC__
25#include <dir.h>
26#else
27#include <direct.h>
28#endif
29#include "example.h"
30#include "zipver.h"
31
32#ifdef WIN32
33#include <commctrl.h>
34#include <winver.h>
35#else
36#include <ver.h>
37#endif
38
39#ifdef WIN32
40#define ZIP_DLL_NAME "ZIP32.DLL\0"
41#else
42#define ZIP_DLL_NAME "ZIP16.DLL\0"
43#endif
44
45#define DLL_WARNING "Cannot find %s."\
46            " The Dll must be in the application directory, the path, "\
47            "the Windows directory or the Windows System directory."
48#define DLL_VERSION_WARNING "%s has the wrong version number."\
49            " Insure that you have the correct dll's installed, and that "\
50            "an older dll is not in your path or Windows System directory."
51
52int hFile;              /* file handle */
53
54ZCL ZpZCL;
55LPZIPUSERFUNCTIONS lpZipUserFunctions;
56HANDLE hZUF = (HANDLE)NULL;
57HINSTANCE hUnzipDll;
58HANDLE hFileList;
59ZPOPT ZpOpt;
60#ifdef WIN32
61DWORD dwPlatformId = 0xFFFFFFFF;
62#endif
63HINSTANCE hZipDll;
64
65
66/* Forward References */
67_DLL_ZIP ZipArchive;
68_ZIP_USER_FUNCTIONS ZipInit;
69ZIPSETOPTIONS ZipSetOptions;
70
71void FreeUpMemory(void);
72int WINAPI DummyPassword(LPSTR, int, LPCSTR, LPCSTR);
73int WINAPI DummyPrint(char far *, unsigned long);
74int WINAPI WINAPI DummyComment(char far *);
75
76#ifdef WIN32
77BOOL IsNT(VOID);
78#endif
79
80/****************************************************************************
81
82    FUNCTION: Main(int argc, char **argv)
83
84****************************************************************************/
85#ifdef __BORLANDC__
86#  ifdef WIN32
87#pragma argsused
88#  endif
89#endif
90int main(int argc, char **argv)
91{
92LPSTR szFileList;
93char **index, *sz;
94int retcode, i, cc;
95DWORD dwVerInfoSize;
96DWORD dwVerHnd;
97char szFullPath[PATH_MAX];
98#ifdef WIN32
99char *ptr;
100#else
101HFILE hfile;
102OFSTRUCT ofs;
103#endif
104HANDLE  hMem;         /* handle to mem alloc'ed */
105
106if (argc < 3)
107   return 0;           /* Exits if not proper number of arguments */
108
109hZUF = GlobalAlloc( GPTR, (DWORD)sizeof(ZIPUSERFUNCTIONS));
110if (!hZUF)
111   {
112   return 0;
113   }
114lpZipUserFunctions = (LPZIPUSERFUNCTIONS)GlobalLock(hZUF);
115
116if (!lpZipUserFunctions)
117   {
118   GlobalFree(hZUF);
119   return 0;
120   }
121
122lpZipUserFunctions->print = DummyPrint;
123lpZipUserFunctions->password = DummyPassword;
124lpZipUserFunctions->comment = DummyComment;
125
126/* Let's go find the dll */
127#ifdef WIN32
128if (SearchPath(
129    NULL,               /* address of search path               */
130    ZIP_DLL_NAME,       /* address of filename                  */
131    NULL,               /* address of extension                 */
132    PATH_MAX,           /* size, in characters, of buffer       */
133    szFullPath,         /* address of buffer for found filename */
134    &ptr                /* address of pointer to file component */
135   ) == 0)
136#else
137hfile = OpenFile(ZIP_DLL_NAME,  &ofs, OF_SEARCH);
138if (hfile == HFILE_ERROR)
139#endif
140   {
141   char str[256];
142   wsprintf (str, DLL_WARNING, ZIP_DLL_NAME);
143   printf("%s\n", str);
144   FreeUpMemory();
145   return 0;
146   }
147#ifndef WIN32
148else
149   lstrcpy(szFullPath, ofs.szPathName);
150_lclose(hfile);
151#endif
152
153/* Now we'll check the zip dll version information */
154dwVerInfoSize =
155    GetFileVersionInfoSize(szFullPath, &dwVerHnd);
156
157if (dwVerInfoSize)
158   {
159   BOOL  fRet, fRetName;
160   char str[256];
161   LPSTR   lpstrVffInfo; /* Pointer to block to hold info */
162   LPSTR lszVer = NULL;
163   LPSTR lszVerName = NULL;
164   UINT  cchVer = 0;
165
166   /* Get a block big enough to hold the version information */
167   hMem          = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
168   lpstrVffInfo  = GlobalLock(hMem);
169
170   /* Get the version information */
171   GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo);
172   fRet = VerQueryValue(lpstrVffInfo,
173              TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
174               (LPVOID)&lszVer,
175               &cchVer);
176   fRetName = VerQueryValue(lpstrVffInfo,
177               TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
178              (LPVOID)&lszVerName,
179              &cchVer);
180   if (!fRet || !fRetName ||
181      (lstrcmpi(lszVer, ZIP_DLL_VERSION) != 0) ||
182      (lstrcmpi(lszVerName, COMPANY_NAME) != 0))
183      {
184      wsprintf (str, DLL_VERSION_WARNING, ZIP_DLL_NAME);
185      printf("%s\n", str);
186      FreeUpMemory();
187      return 0;
188      }
189   /* free memory */
190   GlobalUnlock(hMem);
191   GlobalFree(hMem);
192   }
193else
194   {
195   char str[256];
196   wsprintf (str, DLL_VERSION_WARNING, ZIP_DLL_NAME);
197   printf("%s\n", str);
198   FreeUpMemory();
199   return 0;
200   }
201/* Okay, now we know that the dll exists, and has the proper version
202 * information in it. We can go ahead and load it.
203 */
204hZipDll = LoadLibrary(ZIP_DLL_NAME);
205#ifndef WIN32
206if (hZipDll > HINSTANCE_ERROR)
207#else
208if (hZipDll != NULL)
209#endif
210   {
211   (_DLL_ZIP)ZipArchive = (_DLL_ZIP)GetProcAddress(hZipDll, "ZpArchive");
212   (ZIPSETOPTIONS)ZipSetOptions = (ZIPSETOPTIONS)GetProcAddress(hZipDll, "ZpSetOptions");
213   if (!ZipArchive || !ZipSetOptions)
214      {
215      char str[256];
216      wsprintf (str, "Could not get entry point to %s", ZIP_DLL_NAME);
217      MessageBox((HWND)NULL, str, "Info-ZIP Example", MB_ICONSTOP | MB_OK);
218      FreeUpMemory();
219      return 0;
220      }
221   }
222else
223   {
224   char str[256];
225   wsprintf (str, "Could not load %s", ZIP_DLL_NAME);
226   printf("%s\n", str);
227   FreeUpMemory();
228   return 0;
229   }
230
231(_ZIP_USER_FUNCTIONS)ZipInit = (_ZIP_USER_FUNCTIONS)GetProcAddress(hZipDll, "ZpInit");
232if (!ZipInit)
233   {
234   printf("Cannot get address of ZpInit in Zip dll. Terminating...");
235   FreeLibrary(hZipDll);
236   FreeUpMemory();
237   return 0;
238   }
239if (!(*ZipInit)(lpZipUserFunctions))
240   {
241   printf("Application functions not set up properly. Terminating...");
242   FreeLibrary(hZipDll);
243   FreeUpMemory();
244   return 0;
245   }
246
247/* Here is where the action starts */
248ZpOpt.fSuffix = FALSE;        /* include suffixes (not yet implemented) */
249ZpOpt.fEncrypt = FALSE;       /* true if encryption wanted */
250ZpOpt.fSystem = FALSE;        /* true to include system/hidden files */
251ZpOpt.fVolume = FALSE;        /* true if storing volume label */
252ZpOpt.fExtra = FALSE;         /* true if including extra attributes */
253ZpOpt.fNoDirEntries = FALSE;  /* true if ignoring directory entries */
254ZpOpt.fVerbose = FALSE;       /* true if full messages wanted */
255ZpOpt.fQuiet = FALSE;         /* true if minimum messages wanted */
256ZpOpt.fCRLF_LF = FALSE;       /* true if translate CR/LF to LF */
257ZpOpt.fLF_CRLF = FALSE;       /* true if translate LF to CR/LF */
258ZpOpt.fJunkDir = FALSE;       /* true if junking directory names */
259ZpOpt.fGrow = FALSE;          /* true if allow appending to zip file */
260ZpOpt.fForce = FALSE;         /* true if making entries using DOS names */
261ZpOpt.fMove = FALSE;          /* true if deleting files added or updated */
262ZpOpt.fUpdate = FALSE;        /* true if updating zip file--overwrite only
263                                  if newer */
264ZpOpt.fFreshen = FALSE;       /* true if freshening zip file--overwrite only */
265ZpOpt.fJunkSFX = FALSE;       /* true if junking sfx prefix*/
266ZpOpt.fLatestTime = FALSE;    /* true if setting zip file time to time of
267                                  latest file in archive */
268ZpOpt.fComment = FALSE;       /* true if putting comment in zip file */
269ZpOpt.fOffsets = FALSE;       /* true if updating archive offsets for sfx
270                                  files */
271ZpOpt.fDeleteEntries = FALSE; /* true if deleting files from archive */
272ZpOpt.fRecurse = 0;           /* subdir recursing mode: 1 = "-r", 2 = "-R" */
273ZpOpt.fRepair = 0;            /* archive repair mode: 1 = "-F", 2 = "-FF" */
274ZpOpt.Date = NULL;            /* Not using, set to NULL pointer */
275getcwd(szFullPath, PATH_MAX); /* Set directory to current directory */
276ZpOpt.szRootDir = szFullPath;
277
278ZpZCL.argc = argc - 2;        /* number of files to archive - adjust for the
279                                  actual number of file names to be added */
280ZpZCL.lpszZipFN = argv[1];    /* archive to be created/updated */
281
282/* Copy over the appropriate portions of argv, basically stripping out argv[0]
283   (name of the executable) and argv[1] (name of the archive file)
284 */
285hFileList = GlobalAlloc( GPTR, 0x10000L);
286if ( hFileList )
287   {
288   szFileList = (char far *)GlobalLock(hFileList);
289   }
290index = (char **)szFileList;
291cc = (sizeof(char *) * ZpZCL.argc);
292sz = szFileList + cc;
293
294for (i = 0; i < ZpZCL.argc; i++)
295    {
296    cc = lstrlen(argv[i+2]);
297    lstrcpy(sz, argv[i+2]);
298    index[i] = sz;
299    sz += (cc + 1);
300    }
301ZpZCL.FNV = (char **)szFileList;  /* list of files to archive */
302
303/* Set the options */
304ZipSetOptions(&ZpOpt);
305
306/* Go zip 'em up */
307retcode = ZipArchive(ZpZCL);
308if (retcode != 0)
309   printf("Error in archiving\n");
310
311GlobalUnlock(hFileList);
312GlobalFree(hFileList);
313FreeUpMemory();
314FreeLibrary(hZipDll);
315return 1;
316}
317
318void FreeUpMemory(void)
319{
320if (hZUF)
321   {
322   GlobalUnlock(hZUF);
323   GlobalFree(hZUF);
324   }
325}
326
327#ifdef WIN32
328/* This simply determines if we are running on NT */
329BOOL IsNT(VOID)
330{
331if(dwPlatformId != 0xFFFFFFFF)
332   return dwPlatformId;
333else
334/* note: GetVersionEx() doesn't exist on WinNT 3.1 */
335   {
336   if(GetVersion() < 0x80000000)
337      {
338      (BOOL)dwPlatformId = TRUE;
339      }
340   else
341      {
342      (BOOL)dwPlatformId = FALSE;
343      }
344    }
345return dwPlatformId;
346}
347#endif
348
349/* Password entry routine - see password.c in the wiz directory for how
350   this is actually implemented in Wiz. If you have an encrypted file,
351   this will probably give you great pain. Note that none of the
352   parameters are being used here, and this will give you warnings.
353 */
354int WINAPI DummyPassword(LPSTR p, int n, LPCSTR m, LPCSTR name)
355{
356return 1;
357}
358
359/* Dummy "print" routine that simply outputs what is sent from the dll */
360int WINAPI DummyPrint(char far *buf, unsigned long size)
361{
362printf("%s", buf);
363return (unsigned int) size;
364}
365
366
367/* Dummy "comment" routine. See comment.c in the wiz directory for how
368   this is actually implemented in Wiz. This will probably cause you
369   great pain if you ever actually make a call into it.
370 */
371int WINAPI DummyComment(char far *szBuf)
372{
373szBuf[0] = '\0';
374return TRUE;
375}
376