XZip.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // XZip.h Version 1.1
  2. //
  3. // Authors: Mark Adler et al. (see below)
  4. //
  5. // Modified by: Lucian Wischik
  6. // [email protected]
  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. // [email protected]
  16. //
  17. // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
  18. // - Changed file names to avoid conflicts with Lucian's files
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Lucian Wischik's comments:
  23. // --------------------------
  24. // THIS FILE is almost entirely based upon code by info-zip.
  25. // It has been modified by Lucian Wischik.
  26. // The original code may be found at http://www.info-zip.org
  27. // The original copyright text follows.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. //
  31. // Original authors' comments:
  32. // ---------------------------
  33. // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
  34. // definitive version of this document should be available at
  35. // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
  36. //
  37. // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  38. //
  39. // For the purposes of this copyright and license, "Info-ZIP" is defined as
  40. // the following set of individuals:
  41. //
  42. // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
  43. // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
  44. // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
  45. // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
  46. // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
  47. // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
  48. // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
  49. //
  50. // This software is provided "as is", without warranty of any kind, express
  51. // or implied. In no event shall Info-ZIP or its contributors be held liable
  52. // for any direct, indirect, incidental, special or consequential damages
  53. // arising out of the use of or inability to use this software.
  54. //
  55. // Permission is granted to anyone to use this software for any purpose,
  56. // including commercial applications, and to alter it and redistribute it
  57. // freely, subject to the following restrictions:
  58. //
  59. // 1. Redistributions of source code must retain the above copyright notice,
  60. // definition, disclaimer, and this list of conditions.
  61. //
  62. // 2. Redistributions in binary form (compiled executables) must reproduce
  63. // the above copyright notice, definition, disclaimer, and this list of
  64. // conditions in documentation and/or other materials provided with the
  65. // distribution. The sole exception to this condition is redistribution
  66. // of a standard UnZipSFX binary as part of a self-extracting archive;
  67. // that is permitted without inclusion of this license, as long as the
  68. // normal UnZipSFX banner has not been removed from the binary or disabled.
  69. //
  70. // 3. Altered versions--including, but not limited to, ports to new
  71. // operating systems, existing ports with new graphical interfaces, and
  72. // dynamic, shared, or static library versions--must be plainly marked
  73. // as such and must not be misrepresented as being the original source.
  74. // Such altered versions also must not be misrepresented as being
  75. // Info-ZIP releases--including, but not limited to, labeling of the
  76. // altered versions with the names "Info-ZIP" (or any variation thereof,
  77. // including, but not limited to, different capitalizations),
  78. // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
  79. // Info-ZIP. Such altered versions are further prohibited from
  80. // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
  81. // of the Info-ZIP URL(s).
  82. //
  83. // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
  84. // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
  85. // own source and binary releases.
  86. //
  87. ///////////////////////////////////////////////////////////////////////////////
  88. #ifndef XZIP_H
  89. #define XZIP_H
  90. // ZIP functions -- for creating zip files
  91. // This file is a repackaged form of the Info-Zip source code available
  92. // at www.info-zip.org. The original copyright notice may be found in
  93. // zip.cpp. The repackaging was done by Lucian Wischik to simplify its
  94. // use in Windows/C++.
  95. #ifndef XUNZIP_H
  96. DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that is being created
  97. #endif
  98. typedef DWORD ZRESULT; // result codes from any of the zip functions. Listed later.
  99. // flag values passed to some functions
  100. #define ZIP_HANDLE 1
  101. #define ZIP_FILENAME 2
  102. #define ZIP_MEMORY 3
  103. #define ZIP_FOLDER 4
  104. ///////////////////////////////////////////////////////////////////////////////
  105. //
  106. // CreateZip()
  107. //
  108. // Purpose: Create a zip archive file
  109. //
  110. // Parameters: z - archive file name if flags is ZIP_FILENAME; for other
  111. // uses see below
  112. // len - for memory (ZIP_MEMORY) should be the buffer size;
  113. // for other uses, should be 0
  114. // flags - indicates usage, see below; for files, this will be
  115. // ZIP_FILENAME
  116. //
  117. // Returns: HZIP - non-zero if zip archive created ok, otherwise 0
  118. //
  119. HZIP CreateZip(void *z, unsigned int len, DWORD flags);
  120. // CreateZip - call this to start the creation of a zip file.
  121. // As the zip is being created, it will be stored somewhere:
  122. // to a pipe: CreateZip(hpipe_write, 0,ZIP_HANDLE);
  123. // in a file (by handle): CreateZip(hfile, 0,ZIP_HANDLE);
  124. // in a file (by name): CreateZip("c:\\test.zip", 0,ZIP_FILENAME);
  125. // in memory: CreateZip(buf, len,ZIP_MEMORY);
  126. // or in pagefile memory: CreateZip(0, len,ZIP_MEMORY);
  127. // The final case stores it in memory backed by the system paging file,
  128. // where the zip may not exceed len bytes. This is a bit friendlier than
  129. // allocating memory with new[]: it won't lead to fragmentation, and the
  130. // memory won't be touched unless needed.
  131. // Note: because pipes don't allow random access, the structure of a zipfile
  132. // created into a pipe is slightly different from that created into a file
  133. // or memory. In particular, the compressed-size of the item cannot be
  134. // stored in the zipfile until after the item itself. (Also, for an item added
  135. // itself via a pipe, the uncompressed-size might not either be known until
  136. // after.) This is not normally a problem. But if you try to unzip via a pipe
  137. // as well, then the unzipper will not know these things about the item until
  138. // after it has been unzipped. Therefore: for unzippers which don't just write
  139. // each item to disk or to a pipe, but instead pre-allocate memory space into
  140. // which to unzip them, then either you have to create the zip not to a pipe,
  141. // or you have to add items not from a pipe, or at least when adding items
  142. // from a pipe you have to specify the length.
  143. ///////////////////////////////////////////////////////////////////////////////
  144. //
  145. // ZipAdd()
  146. //
  147. // Purpose: Add a file to a zip archive
  148. //
  149. // Parameters: hz - handle to an open zip archive
  150. // dstzn - name used inside the zip archive to identify the file
  151. // src - for a file (ZIP_FILENAME) this specifies the filename
  152. // to be added to the archive; for other uses, see below
  153. // len - for memory (ZIP_MEMORY) this specifies the buffer
  154. // length; for other uses, this should be 0
  155. // flags - indicates usage, see below; for files, this will be
  156. // ZIP_FILENAME
  157. //
  158. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  159. //
  160. ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags);
  161. // ZipAdd - call this for each file to be added to the zip.
  162. // dstzn is the name that the file will be stored as in the zip file.
  163. // The file to be added to the zip can come
  164. // from a pipe: ZipAdd(hz,"file.dat", hpipe_read,0,ZIP_HANDLE);
  165. // from a file: ZipAdd(hz,"file.dat", hfile,0,ZIP_HANDLE);
  166. // from a fname: ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat",0,ZIP_FILENAME);
  167. // from memory: ZipAdd(hz,"subdir\\file.dat", buf,len,ZIP_MEMORY);
  168. // (folder): ZipAdd(hz,"subdir", 0,0,ZIP_FOLDER);
  169. // Note: if adding an item from a pipe, and if also creating the zip file itself
  170. // to a pipe, then you might wish to pass a non-zero length to the ZipAdd
  171. // function. This will let the zipfile store the items size ahead of the
  172. // compressed item itself, which in turn makes it easier when unzipping the
  173. // zipfile into a pipe.
  174. ///////////////////////////////////////////////////////////////////////////////
  175. //
  176. // CloseZip()
  177. //
  178. // Purpose: Close an open zip archive
  179. //
  180. // Parameters: hz - handle to an open zip archive
  181. //
  182. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  183. //
  184. ZRESULT CloseZip(HZIP hz);
  185. // CloseZip - the zip handle must be closed with this function.
  186. ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len);
  187. // ZipGetMemory - If the zip was created in memory, via ZipCreate(0,ZIP_MEMORY),
  188. // then this function will return information about that memory block.
  189. // buf will receive a pointer to its start, and len its length.
  190. // Note: you can't add any more after calling this.
  191. unsigned int FormatZipMessage(ZRESULT code, char *buf,unsigned int len);
  192. // FormatZipMessage - given an error code, formats it as a string.
  193. // It returns the length of the error message. If buf/len points
  194. // to a real buffer, then it also writes as much as possible into there.
  195. // These are the result codes:
  196. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  197. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  198. // The following come from general system stuff (e.g. files not openable)
  199. #define ZR_GENMASK 0x0000FF00
  200. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  201. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  202. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  203. #define ZR_WRITE 0x00000400 // a general error writing to the file
  204. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  205. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  206. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  207. #define ZR_READ 0x00000800 // a general error reading the file
  208. // The following come from mistakes on the part of the caller
  209. #define ZR_CALLERMASK 0x00FF0000
  210. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  211. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  212. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  213. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  214. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  215. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  216. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  217. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  218. // The following come from bugs within the zip library itself
  219. #define ZR_BUGMASK 0xFF000000
  220. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  221. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  222. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  223. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  224. // e.g.
  225. //
  226. // (1) Traditional use, creating a zipfile from existing files
  227. // HZIP hz = CreateZip("c:\\temp.zip",0,ZIP_FILENAME);
  228. // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
  229. // ZipAdd(hz,"src2.bmp", "c:\\src2_origfn.bmp",0,ZIP_FILENAME);
  230. // CloseZip(hz);
  231. //
  232. // (2) Memory use, creating an auto-allocated mem-based zip file from various sources
  233. // HZIP hz = CreateZip(0,100000,ZIP_MEMORY);
  234. // // adding a conventional file...
  235. // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
  236. // // adding something from memory...
  237. // char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F);
  238. // ZipAdd(hz,"file.dat", buf,1000,ZIP_MEMORY);
  239. // // adding something from a pipe...
  240. // HANDLE hread,hwrite; CreatePipe(&hread,&write,NULL,0);
  241. // HANDLE hthread = CreateThread(ThreadFunc,(void*)hwrite);
  242. // ZipAdd(hz,"unz3.dat", hread,0,ZIP_HANDLE);
  243. // WaitForSingleObject(hthread,INFINITE);
  244. // CloseHandle(hthread); CloseHandle(hread);
  245. // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
  246. // { HANDLE hwrite = (HANDLE)dat;
  247. // char buf[1000]={17};
  248. // DWORD writ; WriteFile(hwrite,buf,1000,&writ,NULL);
  249. // CloseHandle(hwrite);
  250. // return 0;
  251. // }
  252. // // and now that the zip is created, let's do something with it:
  253. // void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
  254. // HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,CREATE_ALWAYS);
  255. // DWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL);
  256. // CloseHandle(hfz);
  257. // CloseZip(hz);
  258. //
  259. // (3) Handle use, for file handles and pipes
  260. // HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite);
  261. // HANDLE hthread = CreateThread(ZipReceiverThread,(void*)hread);
  262. // HZIP hz = ZipCreate(hzwrite,ZIP_HANDLE);
  263. // // ... add to it
  264. // CloseZip(hz);
  265. // CloseHandle(hzwrite);
  266. // WaitForSingleObject(hthread,INFINITE);
  267. // CloseHandle(hthread);
  268. // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
  269. // { HANDLE hread = (HANDLE)dat;
  270. // char buf[1000] = {0};
  271. // while (true)
  272. // { DWORD red = 0; ReadFile(hread,buf,1000,&red,NULL);
  273. // // ... and do something with this zip data we're receiving
  274. // if (red==0) break;
  275. // }
  276. // CloseHandle(hread);
  277. // return 0;
  278. // }
  279. //
  280. // Now we indulge in a little skullduggery so that the code works whether
  281. // the user has included just zip or both zip and unzip.
  282. // Idea: if header files for both zip and unzip are present, then presumably
  283. // the cpp files for zip and unzip are both present, so we will call
  284. // one or the other of them based on a dynamic choice. If the header file
  285. // for only one is present, then we will bind to that particular one.
  286. HZIP CreateZipZ(void *z,unsigned int len,DWORD flags);
  287. ZRESULT CloseZipZ(HZIP hz);
  288. unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len);
  289. bool IsZipHandleZ(HZIP hz);
  290. #define CreateZip CreateZipZ
  291. #ifdef XUNZIP_H
  292. #undef CloseZip
  293. #define CloseZip(hz) (IsZipHandleZ(hz)?CloseZipZ(hz):CloseZipU(hz))
  294. #else
  295. #define CloseZip CloseZipZ
  296. #define FormatZipMessage FormatZipMessageZ
  297. #endif
  298. #endif //XZIP_H