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'

# VPK ファイルを展開する
VPK::VPKFile.new("./path_to.vpk").extract_to("./")

# ディレクトリを 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       # some_dir を some_dir.vpk に圧縮
vpk -x some_file.vpk  # カレントディレクトリに展開

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)

ディレクトリ部の並び(例):
"vtf"                                ← 第 1 段: 拡張子
  "materials/vgui"                   ← 第 2 段: パス
    "logo" + エントリ情報(18 バイト)  ← 第 3 段: ファイル名
    ""                               ← ファイル名の終端
  ""                                 ← パスの終端
""                                   ← 拡張子の終端 = ディレクトリ部おわり

Byte layout of a directory entry (18 bytes)

The read and write flows look like this.

Flow diagram of vpk extraction and compression