ZipFile.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. //========================================================================
  3. // ZipFile.h : API to use Zip files
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. // --------------------------------------------------------------------------
  40. // File: ZipFile.h
  41. //
  42. // Purpose: The declaration of a quick'n dirty ZIP file reader class.
  43. // Original code from Javier Arevalo.
  44. // Get zlib from http://www.cdrom.com/pub/infozip/zlib/
  45. //
  46. // class ZipFile - Chapter 8, page 214
  47. // --------------------------------------------------------------------------
  48. #include <stdio.h>
  49. #include <map>
  50. typedef std::map<std::string, int> ZipContentsMap; // maps path to a zip content id
  51. class ZipFile
  52. {
  53. public:
  54. ZipFile() { m_nEntries=0; m_pFile=NULL; m_pDirData=NULL; }
  55. virtual ~ZipFile() { End(); fclose(m_pFile); }
  56. bool Init(const std::wstring &resFileName);
  57. void End();
  58. int GetNumFiles() const { return m_nEntries; }
  59. std::string GetFilename(int i) const;
  60. int GetFileLen(int i) const;
  61. bool ReadFile(int i, void *pBuf);
  62. // Added to show multi-threaded decompression
  63. bool ReadLargeFile(int i, void *pBuf, void (*progressCallback)(int, bool &));
  64. int Find(const std::string &path) const;
  65. ZipContentsMap m_ZipContentsMap;
  66. private:
  67. struct TZipDirHeader;
  68. struct TZipDirFileHeader;
  69. struct TZipLocalHeader;
  70. FILE *m_pFile; // Zip file
  71. char *m_pDirData; // Raw data buffer.
  72. int m_nEntries; // Number of entries.
  73. // Pointers to the dir entries in pDirData.
  74. const TZipDirFileHeader **m_papDir;
  75. };