example.c 10 KB

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