| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //========================================================================
- // ZipFile.cpp : API to use Zip files
- //
- // Part of the GameCode4 Application
- //
- // GameCode4 is the sample application that encapsulates much of the source code
- // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
- // "Rez" Graham, published by Charles River Media.
- // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
- //
- // If this source code has found it's way to you, and you think it has helped you
- // in any way, do the authors a favor and buy a new copy of the book - there are
- // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
- // by clicking here:
- // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
- //
- // There's a companion web site at http://www.mcshaffry.com/GameCode/
- //
- // The source code is managed and maintained through Google Code:
- // http://code.google.com/p/gamecode4/
- //
- // (c) Copyright 2012 Michael L. McShaffry and David Graham
- //
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser GPL v3
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
- //
- // You should have received a copy of the GNU Lesser GPL v3
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- //
- //========================================================================
- // --------------------------------------------------------------------------
- // File: ZipFile.cpp
- //
- // Purpose: The implementation of a quick'n dirty ZIP file reader class.
- // Original code written by Javier Arevalo.
- // Get zlib from http://www.cdrom.com/pub/infozip/zlib/
- // --------------------------------------------------------------------------
- #include "../Unix/GameCodeStd.h"
- #include "../Utilities/types.h"
- #include <cctype> // for std::tolower
- #include "ZipFile.h"
- #include <zlib.h>
- #include <string.h>
- // --------------------------------------------------------------------------
- // Basic types.
- // --------------------------------------------------------------------------
- typedef unsigned long dword;
- typedef unsigned short word;
- typedef unsigned char byte;
- // --------------------------------------------------------------------------
- // ZIP file structures. Note these have to be packed.
- // --------------------------------------------------------------------------
- #pragma pack(1)
- // --------------------------------------------------------------------------
- // struct ZipFile::TZipLocalHeader - Chapter 8, page 215
- // --------------------------------------------------------------------------
- struct ZipFile::TZipLocalHeader
- {
- enum
- {
- SIGNATURE = 0x04034b50,
- };
- dword sig;
- word version;
- word flag;
- word compression; // Z_NO_COMPRESSION or Z_DEFLATED
- word modTime;
- word modDate;
- dword crc32;
- dword cSize;
- dword ucSize;
- word fnameLen; // Filename string follows header.
- word xtraLen; // Extra field follows filename.
- };
- // --------------------------------------------------------------------------
- // struct ZipFile::TZipDirHeader - Chapter 8, page 215
- // --------------------------------------------------------------------------
- struct ZipFile::TZipDirHeader
- {
- enum
- {
- SIGNATURE = 0x06054b50
- };
- dword sig;
- word nDisk;
- word nStartDisk;
- word nDirEntries;
- word totalDirEntries;
- dword dirSize;
- dword dirOffset;
- word cmntLen;
- };
- // --------------------------------------------------------------------------
- // struct ZipFile::TZipDirFileHeader - Chapter 8, page 215
- // --------------------------------------------------------------------------
- struct ZipFile::TZipDirFileHeader
- {
- enum
- {
- SIGNATURE = 0x02014b50
- };
- dword sig;
- word verMade;
- word verNeeded;
- word flag;
- word compression; // COMP_xxxx
- word modTime;
- word modDate;
- dword crc32;
- dword cSize; // Compressed size
- dword ucSize; // Uncompressed size
- word fnameLen; // Filename string follows header.
- word xtraLen; // Extra field follows filename.
- word cmntLen; // Comment field follows extra field.
- word diskStart;
- word intAttr;
- dword extAttr;
- dword hdrOffset;
- char *GetName () const { return (char *)(this + 1); }
- char *GetExtra () const { return GetName() + fnameLen; }
- char *GetComment() const { return GetExtra() + xtraLen; }
- };
- #pragma pack(0)
- // --------------------------------------------------------------------------
- // Function: Init
- // Purpose: Initialize the object and read the zip file directory.
- // Parameters: A stdio FILE* used for reading.
- // --------------------------------------------------------------------------
- bool ZipFile::Init(const std::wstring &resFileName)
- {
- return false;
- }
- int ZipFile::Find(const std::string &path) const
- {
- std::string lowerCase = path;
- std::transform(lowerCase.begin(), lowerCase.end(), lowerCase.begin(), (int(*)(int)) std::tolower);
- ZipContentsMap::const_iterator i = m_ZipContentsMap.find(lowerCase);
- if (i == m_ZipContentsMap.end())
- return -1;
- return i->second;
- }
- // --------------------------------------------------------------------------
- // Function: End
- // Purpose: Finish the object
- // Parameters:
- // --------------------------------------------------------------------------
- void ZipFile::End()
- {
- m_ZipContentsMap.clear();
- SAFE_DELETE_ARRAY(m_pDirData);
- m_nEntries = 0;
- }
- // --------------------------------------------------------------------------
- // Function: GetFilename
- // Purpose: Return the name of a file
- // Parameters: The file index and the buffer where to store the filename
- // --------------------------------------------------------------------------
- std::string ZipFile::GetFilename(int i) const
- {
- std::string fileName = "";
- if (i >=0 && i < m_nEntries)
- {
- char pszDest[MAX_PATH];
- memcpy(pszDest, m_papDir[i]->GetName(), m_papDir[i]->fnameLen);
- pszDest[m_papDir[i]->fnameLen] = '\0';
- fileName = pszDest;
- }
- return fileName;
- }
- // --------------------------------------------------------------------------
- // Function: GetFileLen
- // Purpose: Return the length of a file so a buffer can be allocated
- // Parameters: The file index.
- // --------------------------------------------------------------------------
- int ZipFile::GetFileLen(int i) const
- {
- if (i < 0 || i >= m_nEntries)
- return -1;
- else
- return m_papDir[i]->ucSize;
- }
- // --------------------------------------------------------------------------
- // Function: ReadFile
- // Purpose: Uncompress a complete file
- // Parameters: The file index and the pre-allocated buffer
- // --------------------------------------------------------------------------
- bool ZipFile::ReadFile(int i, void *pBuf)
- {
- return false;
- }
|