mod compression

module compression

Functions

fn detect_compression(bytes: &[u8]) -> Compression

Detect compression format from the first bytes of a file.

  • 1f 8b = gzip

  • Otherwise = uncompressed

fn detect_compression_from_extension(path: &Path) -> Compression

Detect compression format from a file extension.

Returns Compression::Gzip for .gz extension, Compression::None otherwise.

fn gzip_writer(path: &Path) -> io::Result<flate2::write::GzEncoder<std::fs::File>>

Creates a gzip-compressed writer wrapping a file at the given path.

fn read_file_contents(path: &Path) -> Result<FileContents, Box<dyn std::error::Error>>

Reads file contents, decompressing if needed.

Detection strategy: 1. Read first 2 bytes to check magic bytes. 2. If gzip: decompress entire file to a String. 3. If uncompressed and < 64 KiB: read_to_string. 4. If uncompressed and >= 64 KiB: memory-mapped I/O.

Enums

enum Compression

Detected compression format based on magic bytes.

None
Gzip
enum FileContents

Holds file contents either as an owned String or a memory-mapped region.

Owned(String)
Mapped(memmap2::Mmap)

Implementations

impl FileContents

Functions

fn as_str(&self) -> Result<&str, std::str::Utf8Error>