vpk

VPK ("Valve Pak") is the archive format used by the Source Engine, Valve's game engine (see the Valve Developer Wiki article). This vpk is a Ruby library for reading and writing those VPK files — it can both extract a VPK file and pack a directory into one.

Published as a gem, with a command-line tool bundled in.

gem install vpk
require 'vpk'

# Extract a VPK file
VPK::VPKFile.new("./path_to.vpk").extract_to("./")

# Pack a directory into a VPK
VPK::VPKFile.archive("./path_to_dir").write_to("./archive.vpk")

From the command line it works like this: -c creates a .vpk with the same name as the directory, and -x extracts into the current directory.

vpk -c some_dir       # pack some_dir into some_dir.vpk
vpk -x some_file.vpk  # extract into the current directory

Internals

What it implements is version 1 of the VPK format. A file consists of three layers: a "12-byte header", a "directory section holding the file listing", and a "data section of concatenated file contents".

Byte layout of the header (12 bytes)

Directory section layout (example):
"vtf"                              <- level 1: extension
  "materials/vgui"                 <- level 2: path
    "logo" + entry info (18 bytes) <- level 3: filename
    ""                             <- end of filenames
  ""                               <- end of paths
""                                 <- end of extensions = end of the directory section

Byte layout of a directory entry (18 bytes)

The read and write flows look like this.

Flow diagram of vpk extraction and compression

🤖 This English version is machine-translated by AI.