XUnzip.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // XUnzip.h Version 1.3
  2. //
  3. // Authors: Mark Adler et al. (see below)
  4. //
  5. // Modified by: Lucian Wischik
  6. // lu@wischik.com
  7. //
  8. // Version 1.0 - Turned C files into just a single CPP file
  9. // - Made them compile cleanly as C++ files
  10. // - Gave them simpler APIs
  11. // - Added the ability to zip/unzip directly in memory without
  12. // any intermediate files
  13. //
  14. // Modified by: Hans Dietrich
  15. // hdietrich@gmail.com
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //
  19. // Lucian Wischik's comments:
  20. // --------------------------
  21. // THIS FILE is almost entirely based upon code by info-zip.
  22. // It has been modified by Lucian Wischik.
  23. // The original code may be found at http://www.info-zip.org
  24. // The original copyright text follows.
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. //
  28. // Original authors' comments:
  29. // ---------------------------
  30. // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
  31. // definitive version of this document should be available at
  32. // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
  33. //
  34. // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  35. //
  36. // For the purposes of this copyright and license, "Info-ZIP" is defined as
  37. // the following set of individuals:
  38. //
  39. // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
  40. // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
  41. // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
  42. // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
  43. // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
  44. // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
  45. // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
  46. //
  47. // This software is provided "as is", without warranty of any kind, express
  48. // or implied. In no event shall Info-ZIP or its contributors be held liable
  49. // for any direct, indirect, incidental, special or consequential damages
  50. // arising out of the use of or inability to use this software.
  51. //
  52. // Permission is granted to anyone to use this software for any purpose,
  53. // including commercial applications, and to alter it and redistribute it
  54. // freely, subject to the following restrictions:
  55. //
  56. // 1. Redistributions of source code must retain the above copyright notice,
  57. // definition, disclaimer, and this list of conditions.
  58. //
  59. // 2. Redistributions in binary form (compiled executables) must reproduce
  60. // the above copyright notice, definition, disclaimer, and this list of
  61. // conditions in documentation and/or other materials provided with the
  62. // distribution. The sole exception to this condition is redistribution
  63. // of a standard UnZipSFX binary as part of a self-extracting archive;
  64. // that is permitted without inclusion of this license, as long as the
  65. // normal UnZipSFX banner has not been removed from the binary or disabled.
  66. //
  67. // 3. Altered versions--including, but not limited to, ports to new
  68. // operating systems, existing ports with new graphical interfaces, and
  69. // dynamic, shared, or static library versions--must be plainly marked
  70. // as such and must not be misrepresented as being the original source.
  71. // Such altered versions also must not be misrepresented as being
  72. // Info-ZIP releases--including, but not limited to, labeling of the
  73. // altered versions with the names "Info-ZIP" (or any variation thereof,
  74. // including, but not limited to, different capitalizations),
  75. // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
  76. // Info-ZIP. Such altered versions are further prohibited from
  77. // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
  78. // of the Info-ZIP URL(s).
  79. //
  80. // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
  81. // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
  82. // own source and binary releases.
  83. //
  84. ///////////////////////////////////////////////////////////////////////////////
  85. #ifndef XUNZIP_H
  86. #define XUNZIP_H
  87. #ifndef XZIP_H
  88. DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
  89. #endif
  90. typedef DWORD ZRESULT;
  91. // return codes from any of the zip functions. Listed later.
  92. #define ZIP_HANDLE 1
  93. #define ZIP_FILENAME 2
  94. #define ZIP_MEMORY 3
  95. typedef struct
  96. { int index; // index of this file within the zip
  97. char name[MAX_PATH]; // filename within the zip
  98. DWORD attr; // attributes, as in GetFileAttributes.
  99. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  100. long comp_size; // sizes of item, compressed and uncompressed. These
  101. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  102. } ZIPENTRY;
  103. typedef struct
  104. { int index; // index of this file within the zip
  105. TCHAR name[MAX_PATH]; // filename within the zip
  106. DWORD attr; // attributes, as in GetFileAttributes.
  107. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  108. long comp_size; // sizes of item, compressed and uncompressed. These
  109. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  110. } ZIPENTRYW;
  111. ///////////////////////////////////////////////////////////////////////////////
  112. //
  113. // OpenZip()
  114. //
  115. // Purpose: Open an existing zip archive file
  116. //
  117. // Parameters: z - archive file name if flags is ZIP_FILENAME; for other
  118. // uses see below
  119. // len - for memory (ZIP_MEMORY) should be the buffer size;
  120. // for other uses, should be 0
  121. // flags - indicates usage, see below; for files, this will be
  122. // ZIP_FILENAME
  123. //
  124. // Returns: HZIP - non-zero if zip archive opened ok, otherwise 0
  125. //
  126. HZIP OpenZip(void *z, unsigned int len, DWORD flags);
  127. // OpenZip - opens a zip file and returns a handle with which you can
  128. // subsequently examine its contents. You can open a zip file from:
  129. // from a pipe: OpenZip(hpipe_read,0, ZIP_HANDLE);
  130. // from a file (by handle): OpenZip(hfile,0, ZIP_HANDLE);
  131. // from a file (by name): OpenZip("c:\\test.zip",0, ZIP_FILENAME);
  132. // from a memory block: OpenZip(bufstart, buflen, ZIP_MEMORY);
  133. // If the file is opened through a pipe, then items may only be
  134. // accessed in increasing order, and an item may only be unzipped once,
  135. // although GetZipItem can be called immediately before and after unzipping
  136. // it. If it's opened i n any other way, then full random access is possible.
  137. // Note: pipe input is not yet implemented.
  138. ///////////////////////////////////////////////////////////////////////////////
  139. //
  140. // GetZipItem()
  141. //
  142. // Purpose: Get information about an item in an open zip archive
  143. //
  144. // Parameters: hz - handle of open zip archive
  145. // index - index number (0 based) of item in zip
  146. // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
  147. // (if Unicode)
  148. //
  149. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  150. //
  151. #ifdef _UNICODE
  152. #define GetZipItem GetZipItemW
  153. #else
  154. #define GetZipItem GetZipItemA
  155. #endif
  156. ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
  157. ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
  158. // GetZipItem - call this to get information about an item in the zip.
  159. // If index is -1 and the file wasn't opened through a pipe,
  160. // then it returns information about the whole zipfile
  161. // (and in particular ze.index returns the number of index items).
  162. // Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
  163. // See below for notes on what happens when you unzip such an item.
  164. // Note: if you are opening the zip through a pipe, then random access
  165. // is not possible and GetZipItem(-1) fails and you can't discover the number
  166. // of items except by calling GetZipItem on each one of them in turn,
  167. // starting at 0, until eventually the call fails. Also, in the event that
  168. // you are opening through a pipe and the zip was itself created into a pipe,
  169. // then then comp_size and sometimes unc_size as well may not be known until
  170. // after the item has been unzipped.
  171. ///////////////////////////////////////////////////////////////////////////////
  172. //
  173. // FindZipItem()
  174. //
  175. // Purpose: Find item by name and return information about it
  176. //
  177. // Parameters: hz - handle of open zip archive
  178. // name - name of file to look for inside zip archive
  179. // ic - TRUE = case insensitive
  180. // index - pointer to index number returned, or -1
  181. // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
  182. // (if Unicode)
  183. //
  184. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  185. //
  186. #ifdef _UNICODE
  187. #define FindZipItem FindZipItemW
  188. #else
  189. #define FindZipItem FindZipItemA
  190. #endif
  191. ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
  192. ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
  193. // FindZipItem - finds an item by name. ic means 'insensitive to case'.
  194. // It returns the index of the item, and returns information about it.
  195. // If nothing was found, then index is set to -1 and the function returns
  196. // an error code.
  197. ///////////////////////////////////////////////////////////////////////////////
  198. //
  199. // UnzipItem()
  200. //
  201. // Purpose: Find item by index and unzip it
  202. //
  203. // Parameters: hz - handle of open zip archive
  204. // index - index number of file to unzip
  205. // dst - target file name of unzipped file
  206. // len - for memory (ZIP_MEMORY. length of buffer;
  207. // otherwise 0
  208. // flags - indicates usage, see below; for files, this will be
  209. // ZIP_FILENAME
  210. //
  211. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  212. //
  213. ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
  214. // UnzipItem - given an index to an item, unzips it. You can unzip to:
  215. // to a pipe: UnzipItem(hz,i, hpipe_write,0,ZIP_HANDLE);
  216. // to a file (by handle): UnzipItem(hz,i, hfile,0,ZIP_HANDLE);
  217. // to a file (by name): UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
  218. // to a memory block: UnzipItem(hz,i, buf,buflen,ZIP_MEMORY);
  219. // In the final case, if the buffer isn't large enough to hold it all,
  220. // then the return code indicates that more is yet to come. If it was
  221. // large enough, and you want to know precisely how big, GetZipItem.
  222. // Note: zip files are normally stored with relative pathnames. If you
  223. // unzip with ZIP_FILENAME a relative pathname then the item gets created
  224. // relative to the current directory - it first ensures that all necessary
  225. // subdirectories have been created. Also, the item may itself be a directory.
  226. // If you unzip a directory with ZIP_FILENAME, then the directory gets created.
  227. // If you unzip it to a handle or a memory block, then nothing gets created
  228. // and it emits 0 bytes.
  229. ///////////////////////////////////////////////////////////////////////////////
  230. //
  231. // CloseZip()
  232. //
  233. // Purpose: Close an open zip archive
  234. //
  235. // Parameters: hz - handle to an open zip archive
  236. //
  237. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  238. //
  239. ZRESULT CloseZip(HZIP hz);
  240. // CloseZip - the zip handle must be closed with this function.
  241. unsigned int FormatZipMessage(ZRESULT code, char *buf,unsigned int len);
  242. // FormatZipMessage - given an error code, formats it as a string.
  243. // It returns the length of the error message. If buf/len points
  244. // to a real buffer, then it also writes as much as possible into there.
  245. // These are the result codes:
  246. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  247. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  248. // The following come from general system stuff (e.g. files not openable)
  249. #define ZR_GENMASK 0x0000FF00
  250. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  251. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  252. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  253. #define ZR_WRITE 0x00000400 // a general error writing to the file
  254. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  255. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  256. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  257. #define ZR_READ 0x00000800 // a general error reading the file
  258. // The following come from mistakes on the part of the caller
  259. #define ZR_CALLERMASK 0x00FF0000
  260. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  261. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  262. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  263. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  264. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  265. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  266. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  267. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  268. // The following come from bugs within the zip library itself
  269. #define ZR_BUGMASK 0xFF000000
  270. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  271. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  272. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  273. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  274. // e.g.
  275. //
  276. // SetCurrentDirectory("c:\\docs\\stuff");
  277. // HZIP hz = OpenZip("c:\\stuff.zip",0,ZIP_FILENAME);
  278. // ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
  279. // for (int i=0; i<numitems; i++)
  280. // { GetZipItem(hz,i,&ze);
  281. // UnzipItem(hz,i,ze.name,0,ZIP_FILENAME);
  282. // }
  283. // CloseZip(hz);
  284. //
  285. //
  286. // HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
  287. // HANDLE hglob = LoadResource(hInstance,hrsrc);
  288. // void *zipbuf=LockResource(hglob);
  289. // unsigned int ziplen=SizeofResource(hInstance,hrsrc);
  290. // HZIP hz = OpenZip(zipbuf, ziplen, ZIP_MEMORY);
  291. // - unzip to a membuffer -
  292. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
  293. // char *ibuf = new char[ze.unc_size];
  294. // UnzipItem(hz,i, ibuf, ze.unc_size,ZIP_MEMORY);
  295. // delete[] buf;
  296. // - unzip to a fixed membuff -
  297. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
  298. // char ibuf[1024]; ZIPRESULT zr=ZR_MORE; unsigned long totsize=0;
  299. // while (zr==ZR_MORE)
  300. // { zr = UnzipItem(hz,i, ibuf,1024,ZIP_MEMORY);
  301. // unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
  302. // totsize+=bufsize;
  303. // }
  304. // - unzip to a pipe -
  305. // HANDLE hthread=CreateWavReaderThread(&hread,&hwrite);
  306. // FindZipItem(hz,"sound.wav",&i,&ze);
  307. // UnzipItem(hz,i, hwrite,0,ZIP_HANDLE);
  308. // CloseHandle(hwrite);
  309. // WaitForSingleObject(hthread,INFINITE);
  310. // CloseHandle(hread); CloseHandle(hthread);
  311. // - finished -
  312. // CloseZip(hz);
  313. // // note: no need to free resources obtained through Find/Load/LockResource
  314. //
  315. //
  316. // SetCurrentDirectory("c:\\docs\\pipedzipstuff");
  317. // HANDLE hread,hwrite; CreatePipe(&hread,&hwrite);
  318. // CreateZipWriterThread(hwrite);
  319. // HZIP hz = OpenZip(hread,0,ZIP_HANDLE);
  320. // for (int i=0; ; i++)
  321. // { ZIPENTRY ze; ZRESULT res = GetZipItem(hz,i,&ze);
  322. // if (res!=ZE_OK) break; // no more
  323. // UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
  324. // }
  325. // CloseZip(hz);
  326. //
  327. // Now we indulge in a little skullduggery so that the code works whether
  328. // the user has included just zip or both zip and unzip.
  329. // Idea: if header files for both zip and unzip are present, then presumably
  330. // the cpp files for zip and unzip are both present, so we will call
  331. // one or the other of them based on a dynamic choice. If the header file
  332. // for only one is present, then we will bind to that particular one.
  333. HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
  334. ZRESULT CloseZipU(HZIP hz);
  335. unsigned int FormatZipMessageU(ZRESULT code, char *buf,unsigned int len);
  336. bool IsZipHandleU(HZIP hz);
  337. #define OpenZip OpenZipU
  338. #ifdef XZIP_H
  339. #undef CloseZip
  340. #define CloseZip(hz) (IsZipHandleU(hz)?CloseZipU(hz):CloseZipZ(hz))
  341. #else
  342. #define CloseZip CloseZipU
  343. #define FormatZipMessage FormatZipMessageU
  344. #endif
  345. #endif //XUNZIP_H