unzip.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef _unzip_H
  2. #define _unzip_H
  3. // UNZIPPING functions -- for unzipping.
  4. // This file is a repackaged form of extracts from the zlib code available
  5. // at www.gzip.org/zlib, by Jean-Loup Gailly and Mark Adler. The original
  6. // copyright notice may be found in unzip.cpp. The repackaging was done
  7. // by Lucian Wischik to simplify and extend its use in Windows/C++. Also
  8. // encryption and unicode filenames have been added.
  9. #ifndef _zip_H
  10. DECLARE_HANDLE(HZIP);
  11. #endif
  12. // An HZIP identifies a zip file that has been opened
  13. typedef DWORD ZRESULT;
  14. // return codes from any of the zip functions. Listed later.
  15. typedef struct
  16. { int index; // index of this file within the zip
  17. TCHAR name[MAX_PATH]; // filename within the zip
  18. DWORD attr; // attributes, as in GetFileAttributes.
  19. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  20. long comp_size; // sizes of item, compressed and uncompressed. These
  21. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  22. } ZIPENTRY;
  23. HZIP OpenZip(const TCHAR *fn, const char *password=NULL);
  24. HZIP OpenZip(void *z, unsigned int len, const char *password=NULL);
  25. HZIP OpenZipHandle(HANDLE h, const char *password);
  26. // OpenZip - opens a zip file and returns a handle with which you can
  27. // subsequently examine its contents. You can open a zip file from:
  28. // from a pipe: OpenZipHandle(hpipe_read,0);
  29. // from a file (by handle): OpenZipHandle(hfile,0);
  30. // from a file (by name): OpenZip("c:\\test.zip","password");
  31. // from a memory block: OpenZip(bufstart, buflen,0);
  32. // If the file is opened through a pipe, then items may only be
  33. // accessed in increasing order, and an item may only be unzipped once,
  34. // although GetZipItem can be called immediately before and after unzipping
  35. // it. If it's opened in any other way, then full random access is possible.
  36. // Note: pipe input is not yet implemented.
  37. // Note: zip passwords are ascii, not unicode.
  38. // Note: for windows-ce, you cannot close the handle until after CloseZip.
  39. // but for real windows, the zip makes its own copy of your handle, so you
  40. // can close yours anytime.
  41. ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY *ze);
  42. // GetZipItem - call this to get information about an item in the zip.
  43. // If index is -1 and the file wasn't opened through a pipe,
  44. // then it returns information about the whole zipfile
  45. // (and in particular ze.index returns the number of index items).
  46. // Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
  47. // See below for notes on what happens when you unzip such an item.
  48. // Note: if you are opening the zip through a pipe, then random access
  49. // is not possible and GetZipItem(-1) fails and you can't discover the number
  50. // of items except by calling GetZipItem on each one of them in turn,
  51. // starting at 0, until eventually the call fails. Also, in the event that
  52. // you are opening through a pipe and the zip was itself created into a pipe,
  53. // then then comp_size and sometimes unc_size as well may not be known until
  54. // after the item has been unzipped.
  55. ZRESULT FindZipItem(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
  56. // FindZipItem - finds an item by name. ic means 'insensitive to case'.
  57. // It returns the index of the item, and returns information about it.
  58. // If nothing was found, then index is set to -1 and the function returns
  59. // an error code.
  60. ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn);
  61. ZRESULT UnzipItem(HZIP hz, int index, void *z, unsigned int len);
  62. ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h);
  63. // UnzipItem - given an index to an item, unzips it. You can unzip to:
  64. // to a pipe: UnzipItemHandle(hz,i, hpipe_write);
  65. // to a file (by handle): UnzipItemHandle(hz,i, hfile);
  66. // to a file (by name): UnzipItem(hz,i, ze.name);
  67. // to a memory block: UnzipItem(hz,i, buf,buflen);
  68. // In the final case, if the buffer isn't large enough to hold it all,
  69. // then the return code indicates that more is yet to come. If it was
  70. // large enough, and you want to know precisely how big, GetZipItem.
  71. // Note: zip files are normally stored with relative pathnames. If you
  72. // unzip with ZIP_FILENAME a relative pathname then the item gets created
  73. // relative to the current directory - it first ensures that all necessary
  74. // subdirectories have been created. Also, the item may itself be a directory.
  75. // If you unzip a directory with ZIP_FILENAME, then the directory gets created.
  76. // If you unzip it to a handle or a memory block, then nothing gets created
  77. // and it emits 0 bytes.
  78. ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR *dir);
  79. // if unzipping to a filename, and it's a relative filename, then it will be relative to here.
  80. // (defaults to current-directory).
  81. ZRESULT CloseZip(HZIP hz);
  82. // CloseZip - the zip handle must be closed with this function.
  83. unsigned int FormatZipMessage(ZRESULT code, TCHAR *buf, unsigned int len);
  84. // FormatZipMessage - given an error code, formats it as a string.
  85. // It returns the length of the error message. If buf/len points
  86. // to a real buffer, then it also writes as much as possible into there.
  87. // These are the result codes:
  88. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  89. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  90. // The following come from general system stuff (e.g. files not openable)
  91. #define ZR_GENMASK 0x0000FF00
  92. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  93. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  94. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  95. #define ZR_WRITE 0x00000400 // a general error writing to the file
  96. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  97. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  98. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  99. #define ZR_READ 0x00000800 // a general error reading the file
  100. #define ZR_PASSWORD 0x00001000 // we didn't get the right password to unzip the file
  101. // The following come from mistakes on the part of the caller
  102. #define ZR_CALLERMASK 0x00FF0000
  103. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  104. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  105. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  106. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  107. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  108. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  109. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  110. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  111. // The following come from bugs within the zip library itself
  112. #define ZR_BUGMASK 0xFF000000
  113. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  114. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  115. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  116. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  117. // e.g.
  118. //
  119. // SetCurrentDirectory("c:\\docs\\stuff");
  120. // HZIP hz = OpenZip("c:\\stuff.zip",0);
  121. // ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
  122. // for (int i=0; i<numitems; i++)
  123. // { GetZipItem(hz,i,&ze);
  124. // UnzipItem(hz,i,ze.name);
  125. // }
  126. // CloseZip(hz);
  127. //
  128. //
  129. // HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
  130. // HANDLE hglob = LoadResource(hInstance,hrsrc);
  131. // void *zipbuf=LockResource(hglob);
  132. // unsigned int ziplen=SizeofResource(hInstance,hrsrc);
  133. // HZIP hz = OpenZip(zipbuf, ziplen, 0);
  134. // - unzip to a membuffer -
  135. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
  136. // char *ibuf = new char[ze.unc_size];
  137. // UnzipItem(hz,i, ibuf, ze.unc_size);
  138. // delete[] ibuf;
  139. // - unzip to a fixed membuff -
  140. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
  141. // char ibuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
  142. // while (zr==ZR_MORE)
  143. // { zr = UnzipItem(hz,i, ibuf,1024);
  144. // unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
  145. // totsize+=bufsize;
  146. // }
  147. // - unzip to a pipe -
  148. // HANDLE hwrite; HANDLE hthread=CreateWavReaderThread(&hwrite);
  149. // int i; ZIPENTRY ze; FindZipItem(hz,"sound.wav",true,&i,&ze);
  150. // UnzipItemHandle(hz,i, hwrite);
  151. // CloseHandle(hwrite);
  152. // WaitForSingleObject(hthread,INFINITE);
  153. // CloseHandle(hwrite); CloseHandle(hthread);
  154. // - finished -
  155. // CloseZip(hz);
  156. // // note: no need to free resources obtained through Find/Load/LockResource
  157. //
  158. //
  159. // SetCurrentDirectory("c:\\docs\\pipedzipstuff");
  160. // HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,0,0);
  161. // CreateZipWriterThread(hwrite);
  162. // HZIP hz = OpenZipHandle(hread,0);
  163. // for (int i=0; ; i++)
  164. // { ZIPENTRY ze;
  165. // ZRESULT zr=GetZipItem(hz,i,&ze); if (zr!=ZR_OK) break; // no more
  166. // UnzipItem(hz,i, ze.name);
  167. // }
  168. // CloseZip(hz);
  169. //
  170. //
  171. // Now we indulge in a little skullduggery so that the code works whether
  172. // the user has included just zip or both zip and unzip.
  173. // Idea: if header files for both zip and unzip are present, then presumably
  174. // the cpp files for zip and unzip are both present, so we will call
  175. // one or the other of them based on a dynamic choice. If the header file
  176. // for only one is present, then we will bind to that particular one.
  177. ZRESULT CloseZipU(HZIP hz);
  178. unsigned int FormatZipMessageU(ZRESULT code, TCHAR *buf, unsigned int len);
  179. bool IsZipHandleU(HZIP hz);
  180. #ifdef _zip_H
  181. #undef CloseZip
  182. #define CloseZip(hz) (IsZipHandleU(hz)?CloseZipU(hz):CloseZipZ(hz))
  183. #else
  184. #define CloseZip CloseZipU
  185. #define FormatZipMessage FormatZipMessageU
  186. #endif
  187. #endif // _unzip_H