tree_magic is a Rust crate that determines the MIME type a given file or byte stream.
tree_magic is designed to be more efficient and to have less false positives compared
to the old approach used by
libmagic, or old-fashioned file extension comparisons.
Instead, this loads all known MIME types into a tree based on subclasses. Then, instead
of checking against every file type,
tree_magic will traverse down the tree and
only check the files that make sense to check.
- Very fast perfomance (~150ns to check one file against one type, between 5,000ns and 100,000ns to find a MIME type.)
- Check if a file is a certain type.
- Handles aliases (ex:
- Uses system FreeDesktop.org magic files on Linux systems, and built-in magic file on Windows and macOS.
- Can delegate different file types to different "checkers", reducing false positives by choosing a different method of attack.
cli: Enable building of
staticmime: Change output of all
from_* functions from
Disables ability to load system magic files. Slightly faster.
extern crate tree_magic; // Load a GIF file let input: &[u8] = include_bytes!("../tests/image/gif"); // Find the MIME type of the GIF let result = tree_magic::from_u8(input); assert_eq!(result, "image/gif"); // Check if the MIME and the file are a match let result = tree_magic::match_u8("image/gif", input); assert_eq!(result, true);
The TypeStruct autogenerated at library init, and used by the library.
Information about currently loaded MIME types
Cache used for each checker for each file
Gets the type of a file from a filepath.
Gets the type of a file from a filepath, starting at a certain node in the type graph.
Gets the type of a file from a raw bytestream, starting at a certain node in the type graph.
Gets the type of a file from a byte stream.
Determines if a MIME is an alias of another MIME
Check if the given filepath matches the given MIME type.
Checks if the given bytestream matches the given MIME type.