21 releases (7 breaking)

new 0.7.0 Feb 10, 2025
0.5.2 Feb 6, 2025

#223 in Filesystem

Download history 404/week @ 2024-12-30 747/week @ 2025-01-06 420/week @ 2025-01-13 215/week @ 2025-01-20 2310/week @ 2025-01-27 677/week @ 2025-02-03

3,663 downloads per month
Used in 4 crates (via rsql_drivers)

Apache-2.0 OR MIT

21MB
626K SLoC

file_type

ci Documentation Code Coverage Benchmarks Latest version License Semantic Versioning

Getting Started

File types are determined by examining a file or bytes against known file signatures and file extensions.

Signature, extension and media type data are provided by:

Example

Detect the file type from bytes:

use file_type::FileType;

let file_type = FileType::from_bytes(b"\xCA\xFE\xBA\xBE");
assert_eq!(file_type.extensions(), vec!["class"]);

Retrieve a file type from an extension:

use file_type::FileType;

let file_types = FileType::from_extension("png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.media_types(), vec!["image/png"]);

Retrieve a file type from a media type:

use file_type::FileType;

let file_types = FileType::from_media_type("image/png");
let file_type = file_types.first().expect("file format");
assert_eq!(file_type.extensions(), vec!["png"]);

Feature flags

Name Description Default?
custom Enables custom file types Yes
httpd Enables Apache HTTPD file types No
iana Enables IANA file types No
linguist Enables Linguist file types No
pronom Enables PRONOM file types No
std Enables support for the Rust standard library Yes
wikidata Enables Wikidata file types Yes

Supported File Types

List of supported file types

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

Performance

Comparative performance determining file type from bytes

theseus-rs/file-type - Bencher

Comparative performance determining file type from extension

theseus-rs/file-type - Bencher

Comparative performance determining file type from media type

theseus-rs/file-type - Bencher

License

Licensed under either of

Data is licensed under the following licenses:

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

VSCode Development Container
GitHub Codespaces

Dependencies