pak Module

Source code: pak.py

The pak module provides an PakFile class which derives from ArchiveFile and is used to read and write Quake archive data.

vgio.hrot.pak.is_pakfile(filename)

Quickly see if a file is a pak file by checking the magic number.

The filename argument may be a file for file-like object.

Parameters

filename – File to check as string or file-like object.

Returns

True if given file’s magic number is correct.

PakFile Class

class vgio.hrot.pak.PakFile

Class with methods to open, read, close, and list pak files.

Example

Basic usage:

from vgio.quake.pak import PakFile
p = PakFile(file, mode='r')
Parameters
  • file – Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by PakFile.

  • mode – The file mode for the file-like object.

PakFile.__init__(file, mode='r')
PakFile.open(name, mode='r')

Access a member of the archive as a binary file-like object. name can be either the name of a file within the archive or an ArchiveInfo object. The mode parameter, if included, must be ‘r’ (the default) or ‘w’.

open() is also a context manager and supports the with statement:

with ArchiveFile('archive.file') as archive_file:
    with archive_file.open('entry') as entry_file:
        print(entry_file.read())
Parameters
  • name – Name or ArchiveInfo object.

  • mode – File mode to open object.

Returns

A binary file-like object.

Raises
  • ValueError – If mode isn’t ‘r’ or ‘w’.

  • RuntimeError – If file was already closed.

PakFile.close()

Close the archive file. You must call close() before exiting your program or essential records will not be written.

Raises

ValueError – If open writing handles exist.

PakFile.read(name)

Return the bytes of the file name in the archive. name is the name of the file in the archive, or a ArchiveInfo object. The archive must be open for read or append.

Parameters

name – ArchiveInfo name.

Returns

File as bytes.

PakFile.write(filename, arcname=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). The archive must be open with mode ‘w’, or ‘a’.

Parameters
  • filename

  • arcname – Optional. Name of the info object. If omitted filename will be used.

PakFile.writestr(info_or_arcname, data)

Write a file into the archive. The contents is data, which may be either a string or a bytes instance; if it is a string, it is encoded as UTF-8 first. info_or_arcname is either the file name it will be given in the archive, or a ArchiveInfo instance. If it’s an instance, at least the filename must be given. The archive must be opened with mode ‘w’ or ‘a’.

Parameters
  • info_or_arcname

  • data – Data to be written. Either a string or bytes.

PakFile.extract(member, path=None)

Extract a member from the archive to the current working directory; member must be its full name or a ArchiveInfo object. Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or an ArchiveInfo object.

Parameters
  • member – Either the name of the member to extract or a ArchiveInfo instance.

  • path – The directory to extract to. The current working directory will be used if None.

Returns

Path to extracted file.

PakFile.extractall(path=None, members=None)

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist().

Parameters
  • path – The directory to extract to. The current working directory will be used if None.

  • members – The names of the members to extract. This must be a subset of the list returned by namelist(). All members will be extracted if None.

PakFile.getinfo(name)

Return a ArchiveInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.

Parameters

name – AchiveInfo name.

Returns

An ArchiveInfo object.

Raises

KeyError – If no archive item exists for the given name.

PakFile.infolist()

Return a list containing an ArchiveInfo object for each member of the archive. The objects are in the same order as their entries in the actual archive file on disk if an existing archive was opened.

Returns

A sequence of ArchiveInfo objects.

PakFile.namelist()

Return a list of archive members by name.

Returns

A sequence of filenames.

PakInfo Class

class vgio.hrot.pak.PakInfo

Instances of the PakInfo class are returned by the getinfo() and infolist() methods of PakFile objects. Each object stores information about a single member of the PakFile archive.

filename

Name of file.

file_offset

Offset of file in bytes.

file_size

Size of the file in bytes.

PakInfo.__init__(filename, file_offset=0, file_size=0)