Game Development Reference
In-Depth Information
// -
———————————————————————————————————————————————————————————————————————
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()
You should notice a couple of interesting things about the definition of these struc-
tures. First, there is a #pragma pack around the code. This disables anything the C++
compiler might do to optimize the memory speed of these structures, usually by
spreading them out so that each member variable starts on a 4-byte boundary. Any-
time you define a structure that will be stored onto a disk or in a stream, you should
pack them. Another thing is the definition of a special signature for each structure.
The sig member of each structure is set to a known, constant value, and it is written
out to disk. When it is read back in, if the signatures don
t match the known con-
stant value, you can be sure that you have a corrupted file. It won
'
'
t catch everything,
but it is a good defense.
When a Zip file is opened, the class reads the TZipDirHeader structure at the end
of the file. If the signatures match, the file position is set to the beginning of the array
Search WWH ::




Custom Search