Code Monkey home page Code Monkey logo

turboyang.utility.filesignature's Introduction

TurboYang.Utility.FileSignature

Build Status NuGet version (TurboYang.Utility.FileSignature) MIT License

The library for detecting the type of file based on file header signature (magic number). Implementation for .NET Standard 2.0.

Installation

Install with Package Manager:

Install-Package TurboYang.Utility.FileSignature

Usage: Detect file type

You can directly use the following code to detect the file type.

FileSignatureParser parser = new FileSignatureParser();
List<Signature> signatures = parser.Detect("Your file path");
Signature accuratelySignature = signatures.FirstOrDefault();

String name = accuratelySignature.Name;             // Microsoft Word Document
String extension = accuratelySignature.Extension;   // .docx
String mediaType = accuratelySignature.MediaType;   // application/vnd.openxmlformats-officedocument.wordprocessingml.document

The FileSignatureParser.Detect method returns a list of all possible file types, sorted according to the accuracy of the match. The first record in the list is the most accurate match.

Usage: Check file extension

You can use the following code to directly check that the file extension matches the header signature.

FileSignatureParser parser = new FileSignatureParser();

Boolean isMatch = parser.IsMatch("Your file path");

Usage: Register new file type

If you have a new file type, you can register with the following code. Suppose there are file types Microsoft SQL Server 2000 Database, its extension is .mdf, magic number is 01 0F 00 00.

FileSignatureParser parser = new FileSignatureParser();

parser.RegisterSignature(new Signature(".mdf", "Microsoft SQL Server 2000 Database", String.Empty, new GeneralMatcher(new List<(Int32 Offset, Byte?[] MagicNumber)>()
{
    (0, new Byte?[] { 0x01, 0x0F, 0x00, 0x00 })
})));

List of known file types

Name Media Type Extension
Bitmap Image image/bmp .bmp
GIF Image image/gif .gif
JPEG Image image/jpeg .jpg
PNG Image image/png .png
Tagged Image File Format File image/tiff .tif
Windows Media Audio/Video File video/x-ms-wmv .wmv
Windows/DOS Executable File application/x-msdownload .exe
PKZIP Compressed Archive application/x-zip-compressed .zip
WinRAR Compressed Archive .rar
Microsoft Word 97 - 2003 Document application/msword .doc
Microsoft Excel 97-2003 Worksheet application/vnd.ms-excel .xls
Microsoft PowerPoint 97-2003 Presentation application/vnd.ms-powerpoint .ppt
Microsoft Word Document application/vnd.openxmlformats-officedocument.wordprocessingml.document .docx
Microsoft Excel Worksheet application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xlsx
Microsoft PowerPoint Presentation application/vnd.openxmlformats-officedocument.presentationml.presentation .pptx

turboyang.utility.filesignature's People

Stargazers

 avatar

Watchers

 avatar

Forkers

tonyvalenti

turboyang.utility.filesignature's Issues

Are you open to PRs?

Hi @turboyang-cn -
We would like to use this package in a project of ours but we would need to change some of the source code.

Would you be open to a PR that:

  1. Expands the number of signatures
  2. Prefers records over classes
  3. Removes dependancies on reflection and makes this trimmer-friendly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.