r1ch.net forums
* Home Help Search Login Register
r1ch.net  |  General  |  General Discussion  |  Topic: PHP Code to read directory listing from Quake PAK files
Pages: [1]
Print
Author Topic: PHP Code to read directory listing from Quake PAK files  (Read 7687 times)
SquiSHeR
Guest
« on: November 11, 2010, 01:44:44 pm »

Hi,

using classPak - a PHP class for writing PAK files - as an inspration, I managed to develop PHP Code suitable for retrieving PAK file contents. May it be useful to anyone.
Code:
define('PAK_HEADER_FIELD_LENGTH', 12); //3 x 32 bit = 3 x 4 byte
define('PAK_DIR_ENTRY_LENGTH', 64); //56 byte + 4 byte + 4 byte

function getPakContents($pakFile) {
echo 'Reading ' . basename($pakFile) . ' . . .' . PHP_EOL;

$f = fopen($pakFile, "rb");

$pakdata = fread($f, PAK_HEADER_FIELD_LENGTH);
$raw = unpack("V1ident/V1dirOffset/V1dirLen", $pakdata);
if(1262698832 !== $raw['ident']) {
exit('no quake pak file');
}
$dirOffset = $raw['dirOffset'];
$dirLen = $raw['dirLen'];

$numFiles = $dirLen / PAK_DIR_ENTRY_LENGTH;

echo 'Directory contains ' . $numFiles . ' entries:' . PHP_EOL;

fseek($f, $dirOffset);

$files = array();
while($pakdata = fread($f, PAK_DIR_ENTRY_LENGTH)) {
$pos = strpos($pakdata, chr(0));
$files[] = unpack('a' . ( $pos ? (string)$pos : '56' ) . 'fileName/@56/V1fileOffset/V1fileLen', $pakdata);
echo $files[count($files)-1]['fileName'] . PHP_EOL;
}

fclose($f);
}
Logged
Pages: [1]
Print
r1ch.net  |  General  |  General Discussion  |  Topic: PHP Code to read directory listing from Quake PAK files
Jump to:  

Powered by SMF 1.1.19 | SMF © 2013, Simple Machines