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, Compound File Binary (CFB), Extensible Markup Language (XML) and more.
It checks the signature of the file to determine its format. If it is not recognized by its signature, it returns the default file format which is Arbitrary Binary Data (BIN).
Examples
Determines from a file:
use file_format::{FileFormat, Kind};
let format = FileFormat::from_file("fixtures/application/sample.pdf")?;
assert_eq!(format, FileFormat::PortableDocumentFormat);
assert_eq!(format.name(), "Portable Document Format");
assert_eq!(format.short_name(), "PDF");
assert_eq!(format.media_type(), "application/pdf");
assert_eq!(format.extension(), "pdf");
assert_eq!(format.kind(), Kind::Application);
Determines from bytes:
use file_format::{FileFormat, Kind};
let format = FileFormat::from_bytes(&[0xFF, 0xD8, 0xFF]);
assert_eq!(format, FileFormat::JointPhotographicExpertsGroup);
assert_eq!(format.name(), "Joint Photographic Experts Group");
assert_eq!(format.short_name(), "JPEG");
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.14"
Crate features
All features below are disabled by default.
Accuracy features
These features are only relevant if the associated reader is enabled. They improve the accuracy of detection for specific file formats, but may increase processing time and memory usage.
accuracy
- Enables all accuracy features.accuracy-mkv
- Improves the accuracy of Matroska Video (MKV) based file formats detection.accuracy-pdf
- Improves the accuracy of Portable Document Format (PDF) based file formats detection.accuracy-txt
- Improves the accuracy of Plain Text (TXT) detection.accuracy-xml
- Improves the accuracy of Extensible Markup Language (XML) based file formats detection.accuracy-zip
- Improves the accuracy of ZIP-based file formats detection.
Reader features
These features enable the detection of file formats based on other ones by reading their content.
reader
- Enables all reader features.reader-cfb
- Enables Compound File Binary (CFB) based file formats detection:reader-exe
- Enables MS-DOS Executable (EXE) based file formats detection:reader-mkv
- Enables Matroska Video (MKV) based file formats detection:reader-pdf
- Enables Portable Document Format (PDF) based file formats detection:reader-txt
- Enables Plain Text (TXT) detection when the file format is not recognized by its signature.reader-xml
- Enables Extensible Markup Language (XML) based file formats detection:- Digital Asset Exchange (DAE)
- Extensible 3D Graphics (X3D)
- Extensible Stylesheet Language Transformations (XSLT)
- GPS Exchange Format (GPX)
- Geography Markup Language (GML)
- Keyhole Markup Language (KML)
- MusicXML
- Really Simple Syndication (RSS)
- Scalable Vector Graphics (SVG)
- Simple Object Access Protocol (SOAP)
- XML Localization Interchange File Format (XLIFF)
- draw.io (DRAWIO)
reader-zip
- Enables ZIP-based file formats detection:- 3D Manufacturing Format (3MF)
- Android Package (APK)
- Circuit Diagram Document (CDDX)
- Design Web Format XPS (DWFX)
- Electronic Publication (EPUB)
- Enterprise Application Archive (EAR)
- InDesign Markup Language (IDML)
- Java Archive (JAR)
- Keyhole Markup Language Zipped (KMZ)
- Microsoft Visual Studio Extension (VSIX)
- MusicXML Zipped (MXL)
- Office Open XML Document (DOCX)
- Office Open XML Drawing (VSDX)
- Office Open XML Presentation (PPTX)
- Office Open XML Spreadsheet (XLSX)
- OpenDocument Graphics (ODG)
- OpenDocument Presentation (ODP)
- OpenDocument Spreadsheet (ODS)
- OpenDocument Text (ODT)
- OpenRaster (ORA)
- Web Application Archive (WAR)
- Windows App Package (APPX)
- XAP
- XPInstall (XPI)
- iOS App Store Package (IPA)
Enums
- A file format.
- A kind of
FileFormat
according to the media type.