Crate file_format
source ·Expand description
Crate for determining the file format of a given file or stream.
It provides a variety of functions for identifying a wide range of file formats, including ZIP, CFB, XML and more.
It checks the signature of the file to determine its format. If the file format is not recognized by its signature, it checks if it is Plain Text. Otherwise, it returns the default file format which is Arbitrary Binary Data.
Examples
Determines from a file:
use file_format::{FileFormat, Kind};
let format = FileFormat::from_file("fixtures/application/sample.zip")?;
assert_eq!(format, FileFormat::Zip);
assert_eq!(format.name(), "ZIP");
assert_eq!(format.media_type(), "application/zip");
assert_eq!(format.extension(), "zip");
assert_eq!(format.kind(), Kind::Application);
Determines from bytes:
use file_format::{FileFormat, Kind};
let format = FileFormat::from_bytes(&[0xFF, 0xD8, 0xFF, 0xEE]);
assert_eq!(format, FileFormat::JointPhotographicExpertsGroup);
assert_eq!(format.name(), "Joint Photographic Experts Group");
assert_eq!(format.media_type(), "image/jpeg");
assert_eq!(format.extension(), "jpg");
assert_eq!(format.kind(), Kind::Image);
Usage
Add this to your Cargo.toml
:
[dependencies]
file-format = "0.12"
Features
accuracy
- Improves the accuracy but may increase the processing time and memory usage.cfb
- Enables CFB-based formats support:zip
- Enables ZIP-based formats support:- 3D Manufacturing Format
- Android Package
- Circuit Diagram Document
- Design Web Format XPS
- Electronic Publication
- Enterprise Application Archive
- Java Archive
- Keyhole Markup Language Zipped
- Microsoft Visual Studio Extension
- MusicXML Zipped
- Office Open XML Document
- Office Open XML Drawing
- Office Open XML Presentation
- Office Open XML Spreadsheet
- OpenDocument Graphics
- OpenDocument Presentation
- OpenDocument Spreadsheet
- OpenDocument Text
- OpenRaster
- Web Application Archive
- Windows App Package
- XAP
- XPInstall
- iOS App Store Package
All these features are disabled by default.
Enums
A file format.
A kind of
FileFormat
according to the media type.