A .NET library for detecting MIME types and file extensions based on file signatures and mappings.
$ dotnet add package FileTypeDetectorA .NET library for detecting MIME types and file extensions based on file signatures (magic bytes) and predefined mappings. This project provides a robust way to identify file types from byte arrays, streams, or file paths, supporting both signature-based detection and extension-to-MIME type mappings.
The File Type Detector library is designed to help developers identify file types and MIME types accurately in .NET applications. It supports detection through file signatures (magic bytes) for precise identification and includes a comprehensive mapping of MIME types to file extensions. The library is built with extensibility in mind, allowing developers to add support for additional file formats.
Package Name: File Type Detector
Version: 1.0.0
NuGet Link: https://www.nuget.org/packages/FileTypeDetector
Installation Command:
dotnet add package FileTypeDetector
Add the FileTypeDetector package using the .NET CLI:
dotnet add package FileTypeDetector
using MimeType.Services;
using MimeType.Core.Models;
var detector = new FileSignatureDetector();
byte[] fileBytes = File.ReadAllBytes("sample.png");
var fileTypes = detector.Detect(fileBytes);
foreach (var fileType in fileTypes)
{
Console.WriteLine($"MIME: {fileType.Mime}, Extensions: {string.Join(", ", fileType.Extensions)}");
}
The library currently supports signature-based detection for the following image formats:
0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A.0x49 0x49 0x2A 0x00 (II*) or 0x4D 0x4D 0x00 0x2A (MM*).0xFF 0xD8 0xFF.0xFF 0x0A (raw codestream) or a 12-byte container signature.Additional file formats (e.g., audio, video, and archive formats) are supported for MIME type-to-extension mappings but lack signature detection at this time. Support for these formats will be added in future updates.
using MimeType.Services;
var mimeDetector = new MimeTypeDetector();
var mimeTypes = mimeDetector.Detect(".png");
foreach (var mime in mimeTypes)
{
Console.WriteLine($"MIME Type: {mime}");
}
using MimeType.Services;
var extensionDetector = new FileExtensionDetector();
var extensions = extensionDetector.Detect("image/png");
foreach (var ext in extensions)
{
Console.WriteLine($"Extension: {ext}");
}
[https://github.com/ibrahimekinci/FileType Detector/tree/develop/MimeType.Example](https://github.com/ibrahimekinci/FileType Detector/tree/develop/MimeType.Example)
=== DotNetMimeType Example Console Application ===
=== MIME Type Detection from Extension ===
Extension: .jpg
MIME Type: image/jpeg
=== ************************************* ===
=== Extension Detection from MIME Type ===
MIME Type: image/jpeg
Extensions: jpg, jpeg, jpe, jfif
=== ************************************* ===
=== File Signature Detection from File Path ===
File: [absolute-path]\Files\valid_file (1).png
MIME: image/png, Extensions: png
File: [absolute-path]\Files\valid_file (1).jpg
MIME: image/jpeg, Extensions: jpg, jpeg, jpe, jfif
File: [absolute-path]\Files\valid_file (1).webp
MIME: image/webp, Extensions: webp
=== ************************************* ===
=== File Signature Detection from Stream ===
File: [absolute-path]\Files\valid_file (1).png
MIME: image/png, Extensions: png
File: [absolute-path]\Files\valid_file (1).jpg
MIME: image/jpeg, Extensions: jpg, jpeg, jpe, jfif
File: [absolute-path]\Files\valid_file (1).webp
MIME: image/webp, Extensions: webp
=== ************************************* ===
=== File Signature Detection from Byte Array ===
File: [absolute-path]\Files\valid_file (1).png
MIME: image/png, Extensions: png
File: [absolute-path]\Files\valid_file (1).jpg
MIME: image/jpeg, Extensions: jpg, jpeg, jpe, jfif
File: [absolute-path]\Files\valid_file (1).webp
MIME: image/webp, Extensions: webp
=== ************************************* ===
Press any key to exit...
We welcome contributions to expand the supported file formats and improve the library! To contribute:
MimeType.Infrastructure.FileSignatureCheckers namespace.BuiltInFileTypes class to include new file signatures and mappings.Please ensure your code follows the existing structure and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by Ibrahim Ekinci.