cannot be determined from the provided bytes.
detectFormat
fn
CompressionFormat detectFormat(const(ubyte)[] headerBytes)Detect compression format from header bytes.
This function examines the first few bytes of compressed data to determine the compression format. Different formats have different magic numbers or header signatures that can be used for identification.
Parameters
headerBytes | First N bytes of the compressed file/stream. Typically 6-10 bytes are sufficient for reliable detection. If fewer bytes are provided than needed for a particular format, that format may not be detected. |
Returns
The detected CompressionFormat, or CompressionFormat.AUTO_DETECT if the format
Examples
---
// Detect format from file header File f = File("compressed.gz", "rb"); ubyte[6] header; f.rawRead(header[]); f.seek(0);
auto format = detectFormat(header[]); assert(format == CompressionFormat.GZIP); ---
Note
Format detection is based on magic numbers and header patterns:
- GZIP: 2-byte magic (0x1F 0x8B)
- UNIX .Z: 2-byte magic (0x1F 0x9D)
- BZIP2: 3-byte magic ("BZh")
- LZ4: 4-byte magic (0x04 0x22 0x4D 0x18)
- DEFLATE_ZLIB: 2-byte CMF/FLG with checksum validation
- XZ: 6-byte magic (0xFD 0x37 0x7A 0x58 0x5A 0x00)
- SNAPPY: 10-byte stream identifier (0xFF 0x06 0x00 0x00 "sNaPpY")