Crates.io | filemagic |
lib.rs | filemagic |
version | 0.13.1 |
source | src |
created_at | 2020-12-27 11:24:53.751141 |
updated_at | 2024-10-09 10:26:59.947413 |
description | libmagic bindings |
homepage | https://github.com/marirs/filemagic-rs |
repository | https://github.com/marirs/filemagic-rs |
max_upload_size | |
id | 327790 |
size | 2,936,254 |
filemagic is a Rust wrapper for libmagic, the library that supports the file command on most Unix systems. The package provides a simple Rust API for identifying files using the extensive database of magic strings that ships with libmagic. It can also load a custom database of magic strings.
Rust 1.40.0
or above stable versionlibmagic
brew install libmagic
apt install libmagic1 libmagic-dev
Adding dependency to your Cargo.toml
file
filemagic = "0.13.1"
The vendored
feature uses the cc
crate to compile and
static link a vendored version of libmagic, currently based on 5.45.
Adding dependency to your Cargo.toml
file
filemagic = { version = "0.13.1", features = ["vendored"] }
use filemagic::magic;
fn main() {
let test_file_path = "/path/to/file";
let magic = magic!().expect("error");
println!("{}", magic.file(&test_file_path).expect("error"));
}
use filemagic::magic;
fn main() {
let test_file_path = "/path/to/file";
let databases = vec!["data/db-images-png"];
let magic = magic!(,&databases).expect("error");
println!("{}", magic.file(&test_file_path).expect("error"));
}
use filemagic::Magic;
fn main() {
let test_file_path = "/path/to/file";
// Create a new default configuration
let cookie = Magic::open(Default::default()).expect("error");
cookie.load::<String>(&[]).expect("error");
let magic = cookie.file(&test_file_path).expect("error in magic");
println!("magic= {}", magic);
}
use filemagic::Magic;
fn main() {
// Create a new default configuration
let cookie = Magic::open(Default::default()).expect("error");
// Load one specific magic database
let databases = vec!["data/db-images-png"];
assert!(cookie.load(&databases).is_ok());
// Recognize the magic of a test file
let test_file_path = "data/rust-logo-128x128-blk.png";
let expected_magic = "PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced";
assert_eq!(cookie.file(&test_file_path).unwrap(), expected_magic);
}
cargo doc --release
References: