module documentation
Read and write compressedbuf files, used in NWSync and campaign databases.
Mainly used for NWSync file storage; but also used in sqlite files such as nwsync storage and string/blob compression in campaign sqlite databases.
Binary Format
| Field | Size (bytes) | Description |
|---|---|---|
| Magic | 4 | FileMagic |
| Header Version | 4 | Format version (currently always 3) |
| Algorithm | 4 | Algorithm |
| Uncompressed Size | 4 | Size of the uncompressed data |
| Algorithm Version | 4 (if ZLIB/ZSTD) | Version of the compression algorithm (currently always 1) |
| Dictionary | 4 (if ZSTD) | Dictionary ID (currently always 0) |
| Compressed Data | Variable | The compressed data |
| Class | |
No class docstring; 1/3 constant documented |
| Function | compress |
Compresses the given data using the specified algorithm and returns the compressed data as bytes. |
| Function | decompress |
Decompresses the given data using the specified algorithm and returns the decompressed data as bytes. |
| Function | read |
Decompresses the given binary file using the specified algorithm. |
| Function | write |
Compresses the given data and writes it to the specified file using the specified algorithm. |
| Constant | _VERSION |
Undocumented |
| Constant | _ZLIB |
Undocumented |
| Constant | _ZSTD |
Undocumented |
Compresses the given data using the specified algorithm and returns the compressed data as bytes.
| Parameters | |
data:bytes | The data to be compressed. |
fileFileMagic | The file magic to use. |
alg:Algorithm, optional | The compression algorithm to use. Defaults to Algorithm.ZSTD. |
| Returns | |
bytes | The compressed data as bytes. |
Decompresses the given data using the specified algorithm and returns the decompressed data as bytes.
| Parameters | |
data:bytes | The data to be decompressed. |
expectFileMagic | None | The expected file magic. If provided, the file magic read from the data must match this value. |
| Returns | |
bytes | The decompressed data as bytes. |
def read(file:
BinaryIO, expect_magic: FileMagic | None = None) -> tuple[ bytes, FileMagic, Algorithm]:
(source)
¶
Decompresses the given binary file using the specified algorithm.
| Parameters | |
file:BinaryIO | The binary file to decompress. |
expectFileMagic | None | The expected file magic. If provided, the file magic read from the file must match this value. |
| Returns | |
tuple[ | A tuple containing the decompressed data, the file magic, and the algorithm used. |
| Raises | |
ValueError | If the stream is invalid, or the magic number does not match the expected value, or if an unsupported algorithm is encountered. |
Compresses the given data and writes it to the specified file using the specified algorithm.
| Parameters | |
file:BinaryIO | The file object to write the compressed data to. |
magic:FileMagic | The file magic to write. |
data:bytes | The data to be compressed. |
alg:Algorithm, optional | The compression algorithm to use. Defaults to Algorithm.ZSTD. |
| Raises | |
ValueError | If an unsupported algorithm is specified. |