CZipReader.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __C_ZIP_READER_H_INCLUDED__
  5. #define __C_ZIP_READER_H_INCLUDED__
  6. #include "IrrCompileConfig.h"
  7. #ifdef __IRR_COMPILE_WITH_ZIP_ARCHIVE_LOADER_
  8. #include "IReadFile.h"
  9. #include "irrArray.h"
  10. #include "irrString.h"
  11. #include "IFileSystem.h"
  12. #include "CFileList.h"
  13. namespace irr
  14. {
  15. namespace io
  16. {
  17. // set if the file is encrypted
  18. const s16 ZIP_FILE_ENCRYPTED = 0x0001;
  19. // the fields crc-32, compressed size and uncompressed size are set to
  20. // zero in the local header
  21. const s16 ZIP_INFO_IN_DATA_DESCRIPTOR = 0x0008;
  22. // byte-align structures
  23. #include "irrpack.h"
  24. struct SZIPFileDataDescriptor
  25. {
  26. u32 CRC32;
  27. u32 CompressedSize;
  28. u32 UncompressedSize;
  29. } PACK_STRUCT;
  30. struct SZIPFileHeader
  31. {
  32. u32 Sig; // 'PK0304' little endian (0x04034b50)
  33. s16 VersionToExtract;
  34. s16 GeneralBitFlag;
  35. s16 CompressionMethod;
  36. s16 LastModFileTime;
  37. s16 LastModFileDate;
  38. SZIPFileDataDescriptor DataDescriptor;
  39. s16 FilenameLength;
  40. s16 ExtraFieldLength;
  41. // filename (variable size)
  42. // extra field (variable size )
  43. } PACK_STRUCT;
  44. struct SZIPFileCentralDirFileHeader
  45. {
  46. u32 Sig; // 'PK0102' (0x02014b50)
  47. u16 VersionMadeBy;
  48. u16 VersionToExtract;
  49. u16 GeneralBitFlag;
  50. u16 CompressionMethod;
  51. u16 LastModFileTime;
  52. u16 LastModFileDate;
  53. u32 CRC32;
  54. u32 CompressedSize;
  55. u32 UncompressedSize;
  56. u16 FilenameLength;
  57. u16 ExtraFieldLength;
  58. u16 FileCommentLength;
  59. u16 DiskNumberStart;
  60. u16 InternalFileAttributes;
  61. u32 ExternalFileAttributes;
  62. u32 RelativeOffsetOfLocalHeader;
  63. // filename (variable size)
  64. // extra field (variable size)
  65. // file comment (variable size)
  66. } PACK_STRUCT;
  67. struct SZIPFileCentralDirEnd
  68. {
  69. u32 Sig; // 'PK0506' end_of central dir signature // (0x06054b50)
  70. u16 NumberDisk; // number of this disk
  71. u16 NumberStart; // number of the disk with the start of the central directory
  72. u16 TotalDisk; // total number of entries in the central dir on this disk
  73. u16 TotalEntries; // total number of entries in the central dir
  74. u32 Size; // size of the central directory
  75. u32 Offset; // offset of start of centraldirectory with respect to the starting disk number
  76. u16 CommentLength; // zipfile comment length
  77. // zipfile comment (variable size)
  78. } PACK_STRUCT;
  79. struct SZipFileExtraHeader
  80. {
  81. s16 ID;
  82. s16 Size;
  83. } PACK_STRUCT;
  84. struct SZipFileAESExtraData
  85. {
  86. s16 Version;
  87. u8 Vendor[2];
  88. u8 EncryptionStrength;
  89. s16 CompressionMode;
  90. } PACK_STRUCT;
  91. enum E_GZIP_FLAGS
  92. {
  93. EGZF_TEXT_DAT = 1,
  94. EGZF_CRC16 = 2,
  95. EGZF_EXTRA_FIELDS = 4,
  96. EGZF_FILE_NAME = 8,
  97. EGZF_COMMENT = 16
  98. };
  99. struct SGZIPMemberHeader
  100. {
  101. u16 sig; // 0x8b1f
  102. u8 compressionMethod; // 8 = deflate
  103. u8 flags;
  104. u32 time;
  105. u8 extraFlags; // slow compress = 2, fast compress = 4
  106. u8 operatingSystem;
  107. } PACK_STRUCT;
  108. // Default alignment
  109. #include "irrunpack.h"
  110. //! Contains extended info about zip files in the archive
  111. struct SZipFileEntry
  112. {
  113. //! Position of data in the archive file
  114. s32 Offset;
  115. //! The header for this file containing compression info etc
  116. SZIPFileHeader header;
  117. };
  118. //! Archiveloader capable of loading ZIP Archives
  119. class CArchiveLoaderZIP : public IArchiveLoader
  120. {
  121. public:
  122. //! Constructor
  123. CArchiveLoaderZIP(io::IFileSystem* fs);
  124. //! returns true if the file maybe is able to be loaded by this class
  125. //! based on the file extension (e.g. ".zip")
  126. virtual bool isALoadableFileFormat(const io::path& filename) const;
  127. //! Check if the file might be loaded by this class
  128. /** Check might look into the file.
  129. \param file File handle to check.
  130. \return True if file seems to be loadable. */
  131. virtual bool isALoadableFileFormat(io::IReadFile* file) const;
  132. //! Check to see if the loader can create archives of this type.
  133. /** Check based on the archive type.
  134. \param fileType The archive type to check.
  135. \return True if the archile loader supports this type, false if not */
  136. virtual bool isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const;
  137. //! Creates an archive from the filename
  138. /** \param file File handle to check.
  139. \return Pointer to newly created archive, or 0 upon error. */
  140. virtual IFileArchive* createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const;
  141. //! creates/loads an archive from the file.
  142. //! \return Pointer to the created archive. Returns 0 if loading failed.
  143. virtual io::IFileArchive* createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const;
  144. private:
  145. io::IFileSystem* FileSystem;
  146. };
  147. /*!
  148. Zip file Reader written April 2002 by N.Gebhardt.
  149. */
  150. class CZipReader : public virtual IFileArchive, virtual CFileList
  151. {
  152. public:
  153. //! constructor
  154. CZipReader(IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip=false);
  155. //! destructor
  156. virtual ~CZipReader();
  157. //! opens a file by file name
  158. virtual IReadFile* createAndOpenFile(const io::path& filename);
  159. //! opens a file by index
  160. virtual IReadFile* createAndOpenFile(u32 index);
  161. //! returns the list of files
  162. virtual const IFileList* getFileList() const;
  163. //! get the archive type
  164. virtual E_FILE_ARCHIVE_TYPE getType() const;
  165. protected:
  166. //! reads the next file header from a ZIP file, returns false if there are no more headers.
  167. /* if ignoreGPBits is set, the item will be read despite missing
  168. file information. This is used when reading items from the central
  169. directory. */
  170. bool scanZipHeader(bool ignoreGPBits=false);
  171. //! the same but for gzip files
  172. bool scanGZipHeader();
  173. bool scanCentralDirectoryHeader();
  174. IReadFile* File;
  175. // holds extended info about files
  176. core::array<SZipFileEntry> FileInfo;
  177. bool IsGZip;
  178. };
  179. } // end namespace io
  180. } // end namespace irr
  181. #endif // __IRR_COMPILE_WITH_ZIP_ARCHIVE_LOADER_
  182. #endif // __C_ZIP_READER_H_INCLUDED__