Small crate to infer file and MIME type by checking the magic number signature
Rust
408
240 commits
updated Jul 15, 2026
Small crate to infer file and MIME type by checking the magic number signature.
Adaptation of filetype Go package ported to Rust.
Does not require magic file database (i.e. /etc/magic).
This crate works with Cargo and is on crates.io.
Add it to your Cargo.toml like so:
[dependencies]
infer = "0.22"
If you are not using the custom matcher or the file type from file path functionality you can make this crate even lighter by importing it with no default features, like so:
[dependencies]
infer = { version = "0.3", default-features = false }
This crate supports no_std and no_alloc environments. std support is enabled by default,
but you can disable it by importing the crate with no default features, making it depend
only on the Rust core Library.
alloc has to be enabled to be able to use custom file matchers.
std has to be enabled to be able to get the file type from a file given the file path.
Most operations can be done via top level functions, but they are also available through the Infer
struct, which must be used when dealing custom matchers.
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = infer::get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let kind = infer::get_from_path("testdata/sample.jpg")
.expect("file read successfully")
.expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::image::is_jpeg(&buf));
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::is_image(&buf));
fn custom_matcher(buf: &[u8]) -> bool {
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}
let mut info = infer::Infer::new();
info.add("custom/foo", "foo", custom_matcher);
let buf = [0x10, 0x11, 0x12, 0x13];
let kind = info.get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "custom/foo");
assert_eq!(kind.extension(), "foo");
image/jpegimage/pngimage/gifimage/webpimage/x-canon-cr2image/tiffimage/bmpimage/heifimage/avifimage/vnd.ms-photoimage/vnd.adobe.photoshopimage/vnd.microsoft.iconimage/openrasterimage/vnd.djvuvideo/mp4video/x-m4vvideo/x-matroskavideo/webmvideo/quicktimevideo/x-msvideovideo/x-ms-wmvvideo/mpegvideo/x-flvaudio/midiaudio/mpegaudio/m4aaudio/oggaudio/x-flacaudio/x-wavaudio/amraudio/aacaudio/x-aiffaudio/x-dsfaudio/x-apeapplication/epub+zipapplication/zipapplication/x-tarapplication/vnd.rarapplication/gzipapplication/x-bzip2application/vnd.bzip3application/x-7z-compressedapplication/x-xzapplication/pdfapplication/x-shockwave-flashapplication/rtfapplication/octet-streamapplication/postscriptapplication/vnd.sqlite3application/x-nintendo-nes-romapplication/x-google-chrome-extensionapplication/vnd.ms-cab-compressedapplication/vnd.debian.binary-packageapplication/x-unix-archiveapplication/x-compressapplication/x-lzipapplication/x-rpmapplication/dicomapplication/zstdapplication/x-lz4application/x-ole-storageapplication/x-cpioapplication/x-par2application/epub+zipapplication/x-mobipocket-ebookapplication/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.ms-powerpointapplication/vnd.openxmlformats-officedocument.presentationml.presentationapplication/vnd.oasis.opendocument.textapplication/vnd.oasis.opendocument.spreadsheetapplication/vnd.oasis.opendocument.presentationapplication/font-woffapplication/font-woffapplication/font-sfntapplication/font-sfntapplication/wasmapplication/vnd.microsoft.portable-executableapplication/vnd.microsoft.portable-executableapplication/x-executableapplication/llvmapplication/x-mach-binaryapplication/javaapplication/vnd.android.dexapplication/vnd.android.deyapplication/x-x509-ca-certapplication/x-executableapplication/x-qemu-diskexe and dll have the same magic number so it's not possible to tell which one just based on the binary data. exe is returned for all.MIT
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
137 followers · starred Feb 2025
2,666 followers · starred Oct 2022
131 followers · starred Jul 2025
41 followers · starred Oct 2022
Small crate to infer file and MIME type by checking the magic number signature
Rust
408
240 commits
updated Jul 15, 2026
Small crate to infer file and MIME type by checking the magic number signature.
Adaptation of filetype Go package ported to Rust.
Does not require magic file database (i.e. /etc/magic).
This crate works with Cargo and is on crates.io.
Add it to your Cargo.toml like so:
[dependencies]
infer = "0.22"
If you are not using the custom matcher or the file type from file path functionality you can make this crate even lighter by importing it with no default features, like so:
[dependencies]
infer = { version = "0.3", default-features = false }
This crate supports no_std and no_alloc environments. std support is enabled by default,
but you can disable it by importing the crate with no default features, making it depend
only on the Rust core Library.
alloc has to be enabled to be able to use custom file matchers.
std has to be enabled to be able to get the file type from a file given the file path.
Most operations can be done via top level functions, but they are also available through the Infer
struct, which must be used when dealing custom matchers.
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
let kind = infer::get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let kind = infer::get_from_path("testdata/sample.jpg")
.expect("file read successfully")
.expect("file type is known");
assert_eq!(kind.mime_type(), "image/jpeg");
assert_eq!(kind.extension(), "jpg");
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::image::is_jpeg(&buf));
let buf = [0xFF, 0xD8, 0xFF, 0xAA];
assert!(infer::is_image(&buf));
fn custom_matcher(buf: &[u8]) -> bool {
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
}
let mut info = infer::Infer::new();
info.add("custom/foo", "foo", custom_matcher);
let buf = [0x10, 0x11, 0x12, 0x13];
let kind = info.get(&buf).expect("file type is known");
assert_eq!(kind.mime_type(), "custom/foo");
assert_eq!(kind.extension(), "foo");
image/jpegimage/pngimage/gifimage/webpimage/x-canon-cr2image/tiffimage/bmpimage/heifimage/avifimage/vnd.ms-photoimage/vnd.adobe.photoshopimage/vnd.microsoft.iconimage/openrasterimage/vnd.djvuvideo/mp4video/x-m4vvideo/x-matroskavideo/webmvideo/quicktimevideo/x-msvideovideo/x-ms-wmvvideo/mpegvideo/x-flvaudio/midiaudio/mpegaudio/m4aaudio/oggaudio/x-flacaudio/x-wavaudio/amraudio/aacaudio/x-aiffaudio/x-dsfaudio/x-apeapplication/epub+zipapplication/zipapplication/x-tarapplication/vnd.rarapplication/gzipapplication/x-bzip2application/vnd.bzip3application/x-7z-compressedapplication/x-xzapplication/pdfapplication/x-shockwave-flashapplication/rtfapplication/octet-streamapplication/postscriptapplication/vnd.sqlite3application/x-nintendo-nes-romapplication/x-google-chrome-extensionapplication/vnd.ms-cab-compressedapplication/vnd.debian.binary-packageapplication/x-unix-archiveapplication/x-compressapplication/x-lzipapplication/x-rpmapplication/dicomapplication/zstdapplication/x-lz4application/x-ole-storageapplication/x-cpioapplication/x-par2application/epub+zipapplication/x-mobipocket-ebookapplication/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.ms-powerpointapplication/vnd.openxmlformats-officedocument.presentationml.presentationapplication/vnd.oasis.opendocument.textapplication/vnd.oasis.opendocument.spreadsheetapplication/vnd.oasis.opendocument.presentationapplication/font-woffapplication/font-woffapplication/font-sfntapplication/font-sfntapplication/wasmapplication/vnd.microsoft.portable-executableapplication/vnd.microsoft.portable-executableapplication/x-executableapplication/llvmapplication/x-mach-binaryapplication/javaapplication/vnd.android.dexapplication/vnd.android.deyapplication/x-x509-ca-certapplication/x-executableapplication/x-qemu-diskexe and dll have the same magic number so it's not possible to tell which one just based on the binary data. exe is returned for all.MIT
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
137 followers · starred Feb 2025
2,666 followers · starred Oct 2022
131 followers · starred Jul 2025
41 followers · starred Oct 2022