Crate file_format

source ·
Expand description

Crate for determining the file format of a given file or stream.

It provides a variety of functions for identifying a wide range of file formats, including ZIP, CFB, XML and more.

It checks the signature of the file to determine its format. If the file format is not recognized by its signature, it checks if it is Plain Text. Otherwise, it returns the default file format which is Arbitrary Binary Data.

Examples

Determines from a file:

use file_format::{FileFormat, Kind};

let format = FileFormat::from_file("fixtures/application/sample.zip")?;
assert_eq!(format, FileFormat::Zip);
assert_eq!(format.name(), "ZIP");
assert_eq!(format.media_type(), "application/zip");
assert_eq!(format.extension(), "zip");
assert_eq!(format.kind(), Kind::Application);

Determines from bytes:

use file_format::{FileFormat, Kind};

let format = FileFormat::from_bytes(&[0xFF, 0xD8, 0xFF, 0xEE]);
assert_eq!(format, FileFormat::JointPhotographicExpertsGroup);
assert_eq!(format.name(), "Joint Photographic Experts Group");
assert_eq!(format.media_type(), "image/jpeg");
assert_eq!(format.extension(), "jpg");
assert_eq!(format.kind(), Kind::Image);

Usage

Add this to your Cargo.toml:

[dependencies]
file-format = "0.12"

Features

All these features are disabled by default.

Enums

A file format.
A kind of FileFormat according to the media type.