BSA archive format
Implemented in CreationEngineFormats/ArchiveFile/. Reference:
UESP Archive File Format.
BsaListingReader.ReadAsync reads the blocks in the order the format requires (each offset
depends on the previous block) and returns a flat list of entries. BsaFileExtractor reads one
entry’s data on demand.
| Version | Game | Compression | Folder record |
|---|---|---|---|
| 104 | Skyrim | zlib | 16 bytes |
| 105 | Skyrim Special Edition | LZ4 frame | 24 bytes |
Header
The 36-byte header (Reader/HeaderReader.cs) follows the spec as written. BsaArchiveFlags
lists every archive flag of the spec. The ones the readers act on:
| Flag | Value | Effect |
|---|---|---|
IncludeDirectoryNames | 0x1 | Each file record block starts with the folder name |
IncludeFileNames | 0x2 | The file name block is present |
CompressedArchive | 0x4 | Files are compressed by default |
EmbedFileNames | 0x100 | Each file block starts with the file’s path |
Blocks
| Block | Reader | Layout |
|---|---|---|
| Folder records | FolderRecordsReader | v104: hash (8), file count (4), offset (4). v105 adds 4 padding bytes after the count and after the offset |
| File record blocks | FileRecordBlocksReader | Per folder: name as a bzstring (if 0x1), then one record per file: hash (8), size (4), offset (4) |
| File name block | FileNameBlockReader | One null-terminated name per file, in file record order (if 0x2) |
A folder record’s offset counts the file name block even though that block comes after the file
record blocks: the reader subtracts the total file name length. The file name block is read in one
call of that length, then split on the \0s; \0 padding after the last name is skipped, see
Assumptions. Without a file name
block, an entry is named after its hash.
In a file record’s size, bit 30 (0x40000000) inverts the archive’s default compression for that
file. BsaFileRecord.ActualSize masks it out, IsCompressed(flags) applies it.
File blocks
| Field | Present when |
|---|---|
| Path as a bstring | EmbedFileNames is set |
| uint32 original size | The file is compressed |
| Data | Always |
Version 105 stores each compressed file as a full LZ4 frame (magic 04 22 4D 18), not a raw
LZ4 block. The block decoder LZ4Codec.Decode doesn’t throw on a frame: it returns data of the
right length with the first bytes corrupted. BsaFileExtractor uses the frame decoder
LZ4Stream.Decode.
String tables
.strings, .dlstrings and .ilstrings files live under Strings\, named
<plugin>_<language>.<extension>. Vanilla ships them in Skyrim - Interface.bsa. Reference:
UESP String Table File Format.
SkyrimStringTable.Decode reads them.
| Part | Layout |
|---|---|
| Header | uint32 count, uint32 data size |
| Directory | Per entry: uint32 string id, uint32 offset into the data. Not required to be sorted |
Data (.strings) | Null-terminated strings |
Data (.dlstrings, .ilstrings) | uint32 length, null included, then the string |
Encodings and the fallback rules are in Localization.
Several archives
Vanilla content is split over several archives, and each plugin can add its own. The exporter
merges them into one path index, the archive file system (Game/Bsa/ArchiveFileSystem.cs), see
Load order: when a path is in
several archives, the last one wins, see
Spec deviations.
A loose file in Data overrides every archive, see
Loose files.
Each archive keeps one stream; extraction seeks and reads under a per-archive lock, so entries
can be extracted from several threads.