Dune 2, .PAK files unPAKer in python.

about 15 years ago i wrote an unpacker for dune 2 .pak files (in pascal). i wanted to have the .voc (creative voice files).i decided to take a couple of hours to figure out (again) the format for these pak files and write a python script to unpack them. its probably useless these days but maybe someone can use it 🙂

import struct
import os

def unPAK(filename):
    print "unPAKING:", filename
    fnlist = []
    filesize = os.path.getsize(filename)
    f = open(filename,"rb")
    count = 0;
    size = f.read(4)
    beginData =  struct.unpack('i', size)[0]
    print beginData
    data = f.read(beginData-4)
    data = filter(None, data.split('\x00'))
    print data
    fnlist.append((data[0],beginData))
    for i in range(1,len(data)):
        if ((i % 2) == 1):
            if len(data[i]) == 1:
                print i, data[i]
                pos = struct.unpack('i',data[i]+"\x00\x00\x00")
            elif len(data[i]) == 2:
                print i, data[i]
                pos = struct.unpack('i',data[i]+"\x00\x00")
            elif len(data[i]) == 3:
                print i, data[i]
                pos = struct.unpack('i',data[i]+"\x00")
            fnlist.append((data[i+1],pos[0]))
    print fnlist
    for i in range(1,len(fnlist)):
          total = int(fnlist[i][1]) - int(fnlist[i-1][1])
          print "saving file: ",fnlist[i-1][0], "total bytes", total
          fdata = f.read(total)
          f2 = open(fnlist[i-1][0],"w")
          f2.write(fdata)
          f2.close() 

    #last file.
    print filesize
    total = filesize - int(fnlist[i][1])
    print "saving file: ",fnlist[i][0], "total bytes", total
    f2 = open(fnlist[i][0],"w")
    f2.write(f.read(total))
    f2.close()
    print fnlist

    f.close()

unPAK("DUNE.PAK")
unPAK("ENGLISH.PAK")
unPAK("FINALE.PAK")
unPAK("HARK.PAK")
unPAK("HERC.PAK")
unPAK("INTRO.PAK")
unPAK("INTROVOC.PAK")
unPAK("MENTAT.PAK")
unPAK("MERC.PAK")
unPAK("ORDOS.PAK")
unPAK("SCENARIO.PAK")
unPAK("SOUND.PAK")
unPAK("VOC.PAK")

#unPAK("ATRE.PAK")
#unPAK("XTRE.PAK")