Crate zopen

source ·
Expand description

Simple crate that automatically open compressed files.

The compressor used is determined by the file extension. If the corresponding compression library is not available (i.e. the corresponding feature is not activated), the crate tries to use an external compression tool (gzip, bzip2, xz or zstd).

The crate exports two functions read and [write]. Given a file path, they return a Box<Read> or a Box<Write>, respectively, accessing the file. Depending on the file extension, the file is filtered through an appropriate compressor/decompressor.

Features

The following features enable compression/decompression through external crates. If a features is disabled, zopen uses the corresponding command line tool instead.

  • gzip: enables gzip compression through the flate2 crate. If disabled, uses the external gzip command line tool.
  • bzip2: enables bzip2 compression through the bzip2 crate. If disabled, uses the external bzip2 command line tool.
  • xz: enables xz compression through the rust-lzma crate. If disabled, uses the external xz command line tool.
  • zstd: enables zstd compression through the zstd crate. If disabled, uses the external zstd command line tool.
  • all: enabled all of above.

Example

Reading a compressed file:

let mut f = zopen::read("test.file.gz")?; // open gzip compressed file.
let mut data = String::new();
f.read_to_string(&mut data)?;

Writing to a compressed file:

let mut f = zopen::write("test.file.zst")?; // create zstd compressed file.
writeln!(f, "{}: {}", "Hello world", 42)?;

Functions

  • Open a possibly compressed file for reading.
  • Open a possibly compressed file for writing.