//======================================================================== // 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 // for std::tolower #include "ZipFile.h" #include #include // -------------------------------------------------------------------------- // 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; }