ZipFile.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //========================================================================
  2. // ZipFile.cpp : API to use Zip files
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. // --------------------------------------------------------------------------
  39. // File: ZipFile.cpp
  40. //
  41. // Purpose: The implementation of a quick'n dirty ZIP file reader class.
  42. // Original code written by Javier Arevalo.
  43. // Get zlib from http://www.cdrom.com/pub/infozip/zlib/
  44. // --------------------------------------------------------------------------
  45. #include "../Unix/GameCodeStd.h"
  46. #include "../Utilities/types.h"
  47. #include <cctype> // for std::tolower
  48. #include "ZipFile.h"
  49. #include <zlib.h>
  50. #include <string.h>
  51. // --------------------------------------------------------------------------
  52. // Basic types.
  53. // --------------------------------------------------------------------------
  54. typedef unsigned long dword;
  55. typedef unsigned short word;
  56. typedef unsigned char byte;
  57. // --------------------------------------------------------------------------
  58. // ZIP file structures. Note these have to be packed.
  59. // --------------------------------------------------------------------------
  60. #pragma pack(1)
  61. // --------------------------------------------------------------------------
  62. // struct ZipFile::TZipLocalHeader - Chapter 8, page 215
  63. // --------------------------------------------------------------------------
  64. struct ZipFile::TZipLocalHeader
  65. {
  66. enum
  67. {
  68. SIGNATURE = 0x04034b50,
  69. };
  70. dword sig;
  71. word version;
  72. word flag;
  73. word compression; // Z_NO_COMPRESSION or Z_DEFLATED
  74. word modTime;
  75. word modDate;
  76. dword crc32;
  77. dword cSize;
  78. dword ucSize;
  79. word fnameLen; // Filename string follows header.
  80. word xtraLen; // Extra field follows filename.
  81. };
  82. // --------------------------------------------------------------------------
  83. // struct ZipFile::TZipDirHeader - Chapter 8, page 215
  84. // --------------------------------------------------------------------------
  85. struct ZipFile::TZipDirHeader
  86. {
  87. enum
  88. {
  89. SIGNATURE = 0x06054b50
  90. };
  91. dword sig;
  92. word nDisk;
  93. word nStartDisk;
  94. word nDirEntries;
  95. word totalDirEntries;
  96. dword dirSize;
  97. dword dirOffset;
  98. word cmntLen;
  99. };
  100. // --------------------------------------------------------------------------
  101. // struct ZipFile::TZipDirFileHeader - Chapter 8, page 215
  102. // --------------------------------------------------------------------------
  103. struct ZipFile::TZipDirFileHeader
  104. {
  105. enum
  106. {
  107. SIGNATURE = 0x02014b50
  108. };
  109. dword sig;
  110. word verMade;
  111. word verNeeded;
  112. word flag;
  113. word compression; // COMP_xxxx
  114. word modTime;
  115. word modDate;
  116. dword crc32;
  117. dword cSize; // Compressed size
  118. dword ucSize; // Uncompressed size
  119. word fnameLen; // Filename string follows header.
  120. word xtraLen; // Extra field follows filename.
  121. word cmntLen; // Comment field follows extra field.
  122. word diskStart;
  123. word intAttr;
  124. dword extAttr;
  125. dword hdrOffset;
  126. char *GetName () const { return (char *)(this + 1); }
  127. char *GetExtra () const { return GetName() + fnameLen; }
  128. char *GetComment() const { return GetExtra() + xtraLen; }
  129. };
  130. #pragma pack(0)
  131. // --------------------------------------------------------------------------
  132. // Function: Init
  133. // Purpose: Initialize the object and read the zip file directory.
  134. // Parameters: A stdio FILE* used for reading.
  135. // --------------------------------------------------------------------------
  136. bool ZipFile::Init(const std::wstring &resFileName)
  137. {
  138. return false;
  139. }
  140. int ZipFile::Find(const std::string &path) const
  141. {
  142. std::string lowerCase = path;
  143. std::transform(lowerCase.begin(), lowerCase.end(), lowerCase.begin(), (int(*)(int)) std::tolower);
  144. ZipContentsMap::const_iterator i = m_ZipContentsMap.find(lowerCase);
  145. if (i == m_ZipContentsMap.end())
  146. return -1;
  147. return i->second;
  148. }
  149. // --------------------------------------------------------------------------
  150. // Function: End
  151. // Purpose: Finish the object
  152. // Parameters:
  153. // --------------------------------------------------------------------------
  154. void ZipFile::End()
  155. {
  156. m_ZipContentsMap.clear();
  157. SAFE_DELETE_ARRAY(m_pDirData);
  158. m_nEntries = 0;
  159. }
  160. // --------------------------------------------------------------------------
  161. // Function: GetFilename
  162. // Purpose: Return the name of a file
  163. // Parameters: The file index and the buffer where to store the filename
  164. // --------------------------------------------------------------------------
  165. std::string ZipFile::GetFilename(int i) const
  166. {
  167. std::string fileName = "";
  168. if (i >=0 && i < m_nEntries)
  169. {
  170. char pszDest[MAX_PATH];
  171. memcpy(pszDest, m_papDir[i]->GetName(), m_papDir[i]->fnameLen);
  172. pszDest[m_papDir[i]->fnameLen] = '\0';
  173. fileName = pszDest;
  174. }
  175. return fileName;
  176. }
  177. // --------------------------------------------------------------------------
  178. // Function: GetFileLen
  179. // Purpose: Return the length of a file so a buffer can be allocated
  180. // Parameters: The file index.
  181. // --------------------------------------------------------------------------
  182. int ZipFile::GetFileLen(int i) const
  183. {
  184. if (i < 0 || i >= m_nEntries)
  185. return -1;
  186. else
  187. return m_papDir[i]->ucSize;
  188. }
  189. // --------------------------------------------------------------------------
  190. // Function: ReadFile
  191. // Purpose: Uncompress a complete file
  192. // Parameters: The file index and the pre-allocated buffer
  193. // --------------------------------------------------------------------------
  194. bool ZipFile::ReadFile(int i, void *pBuf)
  195. {
  196. return false;
  197. }