34 个版本
| 0.16.0 | 2024 年 6 月 1 日 |
|---|---|
| 0.15.0 | 2023 年 7 月 5 日 |
| 0.14.0 | 2023 年 6 月 22 日 |
| 0.13.0 | 2023 年 3 月 1 日 |
| 0.1.2 | 2019 年 2 月 23 日 |
#11 in 文件系统
1,560,135 每月下载次数
在 1,281 个 crate (110 个直接使用) 中使用
78KB
2K SLoC
infer
一个小型 crate,通过检查 魔术数字 签名来推断文件和 MIME 类型。
将 filetype Go 包移植到 Rust。
不需要魔术文件数据库(即 /etc/magic)。
功能
- 支持广泛的文件类型
- 提供文件扩展名和 MIME 类型
- 通过扩展名或 MIME 类型查找文件
- 通过类别(图像、视频、音频等)查找文件
- 支持自定义新类型和匹配器
安装
此 crate 与 Cargo 兼容,并在 crates.io 上提供。将其添加到您的 Cargo.toml 中,如下所示
[dependencies]
infer = "0.3"
如果您不使用自定义匹配器或从文件路径获取文件类型的功能,您可以通过不使用默认功能导入此 crate 来使其更轻量,如下所示
[dependencies]
infer = { version = "0.3", default-features = false }
支持 no_std 和 no_alloc
此 crate 支持 no_std 和 no_alloc 环境。默认情况下启用 std 支持,但您可以通过不使用默认功能导入 crate 来禁用它,使其仅依赖于 Rust 的 core 库。
必须启用 alloc 才能使用自定义文件匹配器。
必须启用 std 才能根据文件路径获取文件类型。
示例
大多数操作可以通过 顶级函数 完成,但它们也通过 Infer 结构体提供,当处理自定义匹配器时必须使用它。
获取缓冲区的类型
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");
支持的类型
图像
- jpg -
image/jpeg - png -
image/png - gif -
image/gif - webp -
image/webp - cr2 -
image/x-canon-cr2 - tif -
image/tiff - bmp -
image/bmp - heif -
image/heif - avif -
image/avif - jxr -
image/vnd.ms-photo - psd -
image/vnd.adobe.photoshop - ico -
image/vnd.microsoft.icon - ora -
image/openraster - djvu -
image/vnd.djvu
视频
- mp4 -
video/mp4 - m4v -
video/x-m4v - mkv -
video/x-matroska - webm -
video/webm - mov -
video/quicktime - avi -
video/x-msvideo - wmv -
video/x-ms-wmv - mpg -
video/mpeg - flv -
video/x-flv
音频
- mid -
audio/midi - mp3 -
audio/mpeg - m4a -
audio/m4a - ogg -
audio/ogg - flac -
audio/x-flac - wav -
audio/x-wav - amr -
audio/amr - aac -
audio/aac - aiff -
audio/x-aiff - dsf -
audio/x-dsf - ape -
audio/x-ape
归档
- epub -
application/epub+zip - zip -
application/zip - tar -
application/x-tar - rar -
application/vnd.rar - gz -
application/gzip - bz2 -
application/x-bzip2 - 7z -
application/x-7z-compressed - xz -
application/xz - pdf -
application/pdf - swf -
application/shockwave-flash - rtf -
application/rtf - eot -
application/octet-stream - ps -
application/postscript - sqlite -
application/vnd.sqlite3 - nes -
application/nintendo-nes-rom - crx -
application/chrome-extension - cab -
application/ms-cab-compressed - deb -
application/debian-binary-package - ar -
application/unix-archive - Z -
application/compress - lz -
application/lzip - rpm -
application/rpm - dcm -
application/dicom - zst -
application/zstd - msi -
application/ole-storage - cpio -
application/cpio
书籍
- epub -
application/epub+zip - mobi -
application/mobipocket-ebook
文档
- doc -
application/msword - docx -
application/openxmlformats-officedocument-wordprocessingml-document - xls -
application/ms-excel - xlsx -
application/openxmlformats-officedocument-spreadsheetml-sheet - ppt -
application/ms-powerpoint - pptx -
application/openxmlformats-officedocument-presentationml-presentation - odt -
application/oasis-opendocument-text - ods -
application/oasis-opendocument-spreadsheet - odp -
application/oasis-opendocument-presentation
字体
- woff -
application/font-woff - woff2 -
application/font-woff - ttf -
application/font-sfnt - otf -
application/font-sfnt
应用程序
- wasm -
application/wasm - exe -
application/vnd.microsoft.portable-executable - dll -
application/vnd.microsoft.portable-executable - elf -
application/x-executable - bc -
application/llvm - mach -
application/x-mach-binary - class -
application/java - dex -
application/vnd.android.dex - dey -
application/vnd.android.dey - der -
application/x-x509-ca-cert - obj -
application/x-executable
已知问题
exe和dll具有相同的魔数,因此仅基于二进制数据无法判断是哪一个。exe将被返回。
许可证
MIT
依赖
~110KB