Zstandard dictionary

The compression library Zstandard (also known as "Zstd") has the ability to create an external "dictionary" from a set of training files which can be used to more efficiently (in terms of compression and decompression speed and also in terms of compression ratio) compress files of the same type as the training files. For example, if a dictionary is "trained" on an example set of email messages, anyone with access to the dictionary will be able to more efficiently compress another email file. The trick is that the commonalities are kept in the dictionary file, and, therefore, anyone wishing to decompress the email must have already had that same dictionary sent to them.

Zstandard compression dictionaries each have a 32-bit integer associated with them called the "dictionary ID". Normally, this ID is a completely random value, but the specification says that dictionary IDs less than or equal to 2^15 - 1 = 32767 or greater than or equal to 2^31 = 2147483648 "are reserved [perhaps for technical reasons, or for allocation by an imagined future standards body] and should not be used". If a compression frame uses a dictionary, it will store the dictionary ID, little-endian, in its header.

Part of how dictionaries internally work is by serving as a reference for sequences of bytes that occur both in the real stream and in the dictionary, so that, when a common sequence is encountered during compression, a reference can be made to its position in the dictionary, saving space. This allows for any arbitrarily file of any type to be used as a dictionary, and indeed Zstandard allows for this, but the specialized dictionary format can probably be expected to be more widely-used, especially because it has additional compression-aiding features beyond this. Examination of a test file with a hex editor seems to find that these any-file dictionaries get a dictionary ID field with a length of 0, which the API represents as the integer 0.

Zstandard dictionaries begin with a magic number,, followed by the little-endian dictionary ID. As of May 2019, the dictionary is by default named "dictionary", with no extension. . At the time of writing, there are no Zstd dictionaries that can easily be found on the Web, and it is unclear what convention, if any, there may be in non-public contexts for naming them.