By Ben Owen, 16/08/2009
Revision 1
-------------------------------
.PAK files use a simple archive format to store the majority of the resource files used by the Dune II game engine.
The structure of a .PAK file is very straightforward - a header containing a list of all the files contained within the archive, and a data block containing said files.
HeaderThe header is made up of a series of short structures:
header {
Int32 offset;
char[] filename;
byte terminator = 0;
}
The offset of a contained file is given by a 32-bit, little endian integer, followed by the filename, and finished by a null byte in the format:
00000000:SS SS SS SS NN NN NN NN NN NN NN NN NN NN NN TT �...HATTACK.VOC.
SS = File offset
NN = File name
TT = Null byte
For example, the first two file entries in HARK.PAK:
00000000:aa 02 00 00 48 41 54 54 41 43 4b 2e 56 4f 43 00 �...HATTACK.VOC.
00000010:cc 47 00 00 48 41 52 52 49 56 45 2e 56 4f 43 00 �G..HARRIVE.VOC.
The header is terminated by four null bytes, ie:
00000290:SS NN NN NN NN NN NN NN NN NN NN TT 00 00 00 00 .HNEXT2.VOC.....
Where TT is the null byte terminating the last entry in the header, with the following four null bytes terminating the header itself.
Data
The data block immediately follows the header.
To read a file from the .PAK archive, you should parse the header to find the offset of the file you wish to extract. Subtract this from the offset of the following file (or if this is the last file in the archive, subtract the offset from the archive size) to obtain the file size. You can then perform a binary read operation on the .PAK file starting at the offset and using a buffer of size equal to the file size.