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 
52 int hFile;              /* file handle */
53 
54 ZCL ZpZCL;
55 LPZIPUSERFUNCTIONS lpZipUserFunctions;
56 HANDLE hZUF = (HANDLE)NULL;
57 HINSTANCE hUnzipDll;
58 HANDLE hFileList;
59 ZPOPT ZpOpt;
60 #ifdef WIN32
61 DWORD dwPlatformId = 0xFFFFFFFF;
62 #endif
63 HINSTANCE hZipDll;
64 
65 
66 /* Forward References */
67 _DLL_ZIP ZipArchive;
68 _ZIP_USER_FUNCTIONS ZipInit;
69 ZIPSETOPTIONS ZipSetOptions;
70 
71 void FreeUpMemory(void);
72 int WINAPI DummyPassword(LPSTR, int, LPCSTR, LPCSTR);
73 int WINAPI DummyPrint(char far *, unsigned long);
74 int WINAPI WINAPI DummyComment(char far *);
75 
76 #ifdef WIN32
77 BOOL 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
main(int argc,char ** argv)90 int main(int argc, char **argv)
91 {
92 LPSTR szFileList;
93 char **index, *sz;
94 int retcode, i, cc;
95 DWORD dwVerInfoSize;
96 DWORD dwVerHnd;
97 char szFullPath[PATH_MAX];
98 #ifdef WIN32
99 char *ptr;
100 #else
101 HFILE hfile;
102 OFSTRUCT ofs;
103 #endif
104 HANDLE  hMem;         /* handle to mem alloc'ed */
105 
106 if (argc < 3)
107    return 0;           /* Exits if not proper number of arguments */
108 
109 hZUF = GlobalAlloc( GPTR, (DWORD)sizeof(ZIPUSERFUNCTIONS));
110 if (!hZUF)
111    {
112    return 0;
113    }
114 lpZipUserFunctions = (LPZIPUSERFUNCTIONS)GlobalLock(hZUF);
115 
116 if (!lpZipUserFunctions)
117    {
118    GlobalFree(hZUF);
119    return 0;
120    }
121 
122 lpZipUserFunctions->print = DummyPrint;
123 lpZipUserFunctions->password = DummyPassword;
124 lpZipUserFunctions->comment = DummyComment;
125 
126 /* Let's go find the dll */
127 #ifdef WIN32
128 if (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
137 hfile = OpenFile(ZIP_DLL_NAME,  &ofs, OF_SEARCH);
138 if (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
148 else
149    lstrcpy(szFullPath, ofs.szPathName);
150 _lclose(hfile);
151 #endif
152 
153 /* Now we'll check the zip dll version information */
154 dwVerInfoSize =
155     GetFileVersionInfoSize(szFullPath, &dwVerHnd);
156 
157 if (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    }
193 else
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  */
204 hZipDll = LoadLibrary(ZIP_DLL_NAME);
205 #ifndef WIN32
206 if (hZipDll > HINSTANCE_ERROR)
207 #else
208 if (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    }
222 else
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");
232 if (!ZipInit)
233    {
234    printf("Cannot get address of ZpInit in Zip dll. Terminating...");
235    FreeLibrary(hZipDll);
236    FreeUpMemory();
237    return 0;
238    }
239 if (!(*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 */
248 ZpOpt.fSuffix = FALSE;        /* include suffixes (not yet implemented) */
249 ZpOpt.fEncrypt = FALSE;       /* true if encryption wanted */
250 ZpOpt.fSystem = FALSE;        /* true to include system/hidden files */
251 ZpOpt.fVolume = FALSE;        /* true if storing volume label */
252 ZpOpt.fExtra = FALSE;         /* true if including extra attributes */
253 ZpOpt.fNoDirEntries = FALSE;  /* true if ignoring directory entries */
254 ZpOpt.fVerbose = FALSE;       /* true if full messages wanted */
255 ZpOpt.fQuiet = FALSE;         /* true if minimum messages wanted */
256 ZpOpt.fCRLF_LF = FALSE;       /* true if translate CR/LF to LF */
257 ZpOpt.fLF_CRLF = FALSE;       /* true if translate LF to CR/LF */
258 ZpOpt.fJunkDir = FALSE;       /* true if junking directory names */
259 ZpOpt.fGrow = FALSE;          /* true if allow appending to zip file */
260 ZpOpt.fForce = FALSE;         /* true if making entries using DOS names */
261 ZpOpt.fMove = FALSE;          /* true if deleting files added or updated */
262 ZpOpt.fUpdate = FALSE;        /* true if updating zip file--overwrite only
263                                   if newer */
264 ZpOpt.fFreshen = FALSE;       /* true if freshening zip file--overwrite only */
265 ZpOpt.fJunkSFX = FALSE;       /* true if junking sfx prefix*/
266 ZpOpt.fLatestTime = FALSE;    /* true if setting zip file time to time of
267                                   latest file in archive */
268 ZpOpt.fComment = FALSE;       /* true if putting comment in zip file */
269 ZpOpt.fOffsets = FALSE;       /* true if updating archive offsets for sfx
270                                   files */
271 ZpOpt.fDeleteEntries = FALSE; /* true if deleting files from archive */
272 ZpOpt.fRecurse = 0;           /* subdir recursing mode: 1 = "-r", 2 = "-R" */
273 ZpOpt.fRepair = 0;            /* archive repair mode: 1 = "-F", 2 = "-FF" */
274 ZpOpt.Date = NULL;            /* Not using, set to NULL pointer */
275 getcwd(szFullPath, PATH_MAX); /* Set directory to current directory */
276 ZpOpt.szRootDir = szFullPath;
277 
278 ZpZCL.argc = argc - 2;        /* number of files to archive - adjust for the
279                                   actual number of file names to be added */
280 ZpZCL.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  */
285 hFileList = GlobalAlloc( GPTR, 0x10000L);
286 if ( hFileList )
287    {
288    szFileList = (char far *)GlobalLock(hFileList);
289    }
290 index = (char **)szFileList;
291 cc = (sizeof(char *) * ZpZCL.argc);
292 sz = szFileList + cc;
293 
294 for (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     }
301 ZpZCL.FNV = (char **)szFileList;  /* list of files to archive */
302 
303 /* Set the options */
304 ZipSetOptions(&ZpOpt);
305 
306 /* Go zip 'em up */
307 retcode = ZipArchive(ZpZCL);
308 if (retcode != 0)
309    printf("Error in archiving\n");
310 
311 GlobalUnlock(hFileList);
312 GlobalFree(hFileList);
313 FreeUpMemory();
314 FreeLibrary(hZipDll);
315 return 1;
316 }
317 
FreeUpMemory(void)318 void FreeUpMemory(void)
319 {
320 if (hZUF)
321    {
322    GlobalUnlock(hZUF);
323    GlobalFree(hZUF);
324    }
325 }
326 
327 #ifdef WIN32
328 /* This simply determines if we are running on NT */
IsNT(VOID)329 BOOL IsNT(VOID)
330 {
331 if(dwPlatformId != 0xFFFFFFFF)
332    return dwPlatformId;
333 else
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     }
345 return 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  */
DummyPassword(LPSTR p,int n,LPCSTR m,LPCSTR name)354 int WINAPI DummyPassword(LPSTR p, int n, LPCSTR m, LPCSTR name)
355 {
356 return 1;
357 }
358 
359 /* Dummy "print" routine that simply outputs what is sent from the dll */
DummyPrint(char far * buf,unsigned long size)360 int WINAPI DummyPrint(char far *buf, unsigned long size)
361 {
362 printf("%s", buf);
363 return (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  */
DummyComment(char far * szBuf)371 int WINAPI DummyComment(char far *szBuf)
372 {
373 szBuf[0] = '\0';
374 return TRUE;
375 }
376