Q3BSPZipArchive.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef AI_Q3BSP_ZIPARCHIVE_H_INC
  34. #define AI_Q3BSP_ZIPARCHIVE_H_INC
  35. #include "../contrib/unzip/unzip.h"
  36. #include "../include/assimp/IOStream.hpp"
  37. #include "../include/assimp/IOSystem.hpp"
  38. #include <string>
  39. #include <vector>
  40. #include <map>
  41. #include <cassert>
  42. namespace Assimp
  43. {
  44. namespace Q3BSP
  45. {
  46. // ------------------------------------------------------------------------------------------------
  47. /// \class ZipFile
  48. /// \ingroup Assimp::Q3BSP
  49. ///
  50. /// \brief
  51. // ------------------------------------------------------------------------------------------------
  52. class ZipFile : public IOStream
  53. {
  54. public:
  55. ZipFile( const std::string &rFileName, unzFile zipFile ) :
  56. m_Name( rFileName ),
  57. m_zipFile( zipFile )
  58. {
  59. ai_assert( NULL != m_zipFile );
  60. }
  61. ~ZipFile()
  62. {
  63. m_zipFile = NULL;
  64. }
  65. size_t Read(void* pvBuffer, size_t pSize, size_t pCount )
  66. {
  67. size_t bytes_read = 0;
  68. if ( NULL == m_zipFile )
  69. return bytes_read;
  70. // search file and place file pointer there
  71. if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
  72. {
  73. // get file size, etc.
  74. unz_file_info fileInfo;
  75. unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
  76. const size_t size = pSize * pCount;
  77. assert( size <= fileInfo.uncompressed_size );
  78. // The file has EXACTLY the size of uncompressed_size. In C
  79. // you need to mark the last character with '\0', so add
  80. // another character
  81. unzOpenCurrentFile( m_zipFile );
  82. const int ret = unzReadCurrentFile( m_zipFile, pvBuffer, fileInfo.uncompressed_size);
  83. size_t filesize = fileInfo.uncompressed_size;
  84. if ( ret < 0 || size_t(ret) != filesize )
  85. {
  86. return 0;
  87. }
  88. bytes_read = ret;
  89. unzCloseCurrentFile( m_zipFile );
  90. }
  91. return bytes_read;
  92. }
  93. size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/)
  94. {
  95. return 0;
  96. }
  97. size_t FileSize() const
  98. {
  99. if ( NULL == m_zipFile )
  100. return 0;
  101. if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
  102. {
  103. unz_file_info fileInfo;
  104. unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
  105. return fileInfo.uncompressed_size;
  106. }
  107. return 0;
  108. }
  109. aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/)
  110. {
  111. return aiReturn_FAILURE;
  112. }
  113. size_t Tell() const
  114. {
  115. return 0;
  116. }
  117. void Flush()
  118. {
  119. // empty
  120. }
  121. private:
  122. std::string m_Name;
  123. unzFile m_zipFile;
  124. };
  125. // ------------------------------------------------------------------------------------------------
  126. /// \class Q3BSPZipArchive
  127. /// \ingroup Assimp::Q3BSP
  128. ///
  129. /// \brief IMplements a zip archive like the WinZip archives. Will be also used to import data
  130. /// from a P3K archive ( Quake level format ).
  131. // ------------------------------------------------------------------------------------------------
  132. class Q3BSPZipArchive : public Assimp::IOSystem
  133. {
  134. public:
  135. static const unsigned int FileNameSize = 256;
  136. public:
  137. Q3BSPZipArchive( const std::string & rFile );
  138. ~Q3BSPZipArchive();
  139. bool Exists( const char* pFile) const;
  140. char getOsSeparator() const;
  141. IOStream* Open(const char* pFile, const char* pMode = "rb");
  142. void Close( IOStream* pFile);
  143. bool isOpen() const;
  144. void getFileList( std::vector<std::string> &rFileList );
  145. private:
  146. bool mapArchive();
  147. private:
  148. unzFile m_ZipFileHandle;
  149. std::map<std::string, IOStream*> m_ArchiveMap;
  150. std::vector<std::string> m_FileList;
  151. bool m_bDirty;
  152. };
  153. // ------------------------------------------------------------------------------------------------
  154. } // Namespace Q3BSP
  155. } // Namespace Assimp
  156. #endif // AI_Q3BSP_ZIPARCHIVE_H_INC