#ifndef __C_ZIP_READER_H_INCLUDED__
#define __C_ZIP_READER_H_INCLUDED__
#include "IrrCompileConfig.h"
#ifdef __IRR_COMPILE_WITH_ZIP_ARCHIVE_LOADER_
#include "IReadFile.h"
#include "irrArray.h"
#include "irrString.h"
#include "IFileSystem.h"
#include "CFileList.h"
namespace irr
{
namespace io
{
const s16 ZIP_FILE_ENCRYPTED = 0x0001;
const s16 ZIP_INFO_IN_DATA_DESCRIPTOR = 0x0008;
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
# pragma pack( push, packing )
# pragma pack( 1 )
# define PACK_STRUCT
#elif defined( __GNUC__ )
# define PACK_STRUCT __attribute__((packed))
#else
# error compiler not supported
#endif
struct SZIPFileDataDescriptor
{
u32 CRC32;
u32 CompressedSize;
u32 UncompressedSize;
} PACK_STRUCT;
struct SZIPFileHeader
{
u32 Sig;
s16 VersionToExtract;
s16 GeneralBitFlag;
s16 CompressionMethod;
s16 LastModFileTime;
s16 LastModFileDate;
SZIPFileDataDescriptor DataDescriptor;
s16 FilenameLength;
s16 ExtraFieldLength;
} PACK_STRUCT;
struct SZIPFileCentralDirFileHeader
{
u32 Sig;
u16 VersionMadeBy;
u16 VersionToExtract;
u16 GeneralBitFlag;
u16 CompressionMethod;
u16 LastModFileTime;
u16 LastModFileDate;
u32 CRC32;
u32 CompressedSize;
u32 UncompressedSize;
u16 FilenameLength;
u16 ExtraFieldLength;
u16 FileCommentLength;
u16 DiskNumberStart;
u16 InternalFileAttributes;
u32 ExternalFileAttributes;
u32 RelativeOffsetOfLocalHeader;
} PACK_STRUCT;
struct SZIPFileCentralDirEnd
{
u32 Sig;
u16 NumberDisk;
u16 NumberStart;
u16 TotalDisk;
u16 TotalEntries;
u32 Size;
u32 Offset;
u16 CommentLength;
} PACK_STRUCT;
struct SZipFileExtraHeader
{
s16 ID;
s16 Size;
} PACK_STRUCT;
struct SZipFileAESExtraData
{
s16 Version;
u8 Vendor[2];
u8 EncryptionStrength;
s16 CompressionMode;
} PACK_STRUCT;
enum E_GZIP_FLAGS
{
EGZF_TEXT_DAT = 1,
EGZF_CRC16 = 2,
EGZF_EXTRA_FIELDS = 4,
EGZF_FILE_NAME = 8,
EGZF_COMMENT = 16
};
struct SGZIPMemberHeader
{
u16 sig;
u8 compressionMethod;
u8 flags;
u32 time;
u8 extraFlags;
u8 operatingSystem;
} PACK_STRUCT;
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
# pragma pack( pop, packing )
#endif
#undef PACK_STRUCT
struct SZipFileEntry
{
s32 Offset;
SZIPFileHeader header;
};
class CArchiveLoaderZIP : public IArchiveLoader
{
public:
CArchiveLoaderZIP(io::IFileSystem* fs);
virtual bool isALoadableFileFormat(const io::path& filename) const;
virtual bool isALoadableFileFormat(io::IReadFile* file) const;
virtual bool isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const;
virtual IFileArchive* createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const;
virtual io::IFileArchive* createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const;
private:
io::IFileSystem* FileSystem;
};
class CZipReader : public virtual IFileArchive, virtual CFileList
{
public:
CZipReader(IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip=false);
virtual ~CZipReader();
virtual IReadFile* createAndOpenFile(const io::path& filename);
virtual IReadFile* createAndOpenFile(u32 index);
virtual const IFileList* getFileList() const;
virtual E_FILE_ARCHIVE_TYPE getType() const;
protected:
bool scanZipHeader(bool ignoreGPBits=false);
bool scanGZipHeader();
bool scanCentralDirectoryHeader();
IReadFile* File;
core::array<SZipFileEntry> FileInfo;
bool IsGZip;
};
}
}
#endif
#endif