What is Myrmec?
Myrmec is a library for detecting file formats, Myrmec is different from other libraries or handwriting detection code, MYRMEC does not rely on the file extension (in practice, your users are likely to use a false extension to deceive your application), MYRMEC will detect the file binary header, and match them in their metabase to get the file format.
For example, the binary header of a JPG image is "FF D8 ff DB" then Myrmec will match to this file header and get two results-"JPG" and "JPEG".
How to use
Install the NuGet package first
install-package Myrmec
or through DOTNETCLI.
dotnet Add Package Myrmec
Writing a matching code
1 //creating a sniffer2Sniffer Sniffer =NewSniffer ();3 4 //populating a sniffer with metadata5 Sniffer. Populate (filetypes.commonfiletypes);6 7 //gets the file header to match the file, typically 20 bytes is enough, some formats may take longer8 byte[] Filehead =Readfilehead ();9 Ten //match and get results Onelist<string> results = Sniffer. Match (Filehead)
Why the result is list<string>?
Many times a file format will correspond to multiple extensions, such as JPG. There are times when multiple file formats are file formats, such as Zip, apk, pptx they are all zip achive, so when you match a zip file, you will get at least 3 extensions. When the result is empty, the description does not match to the result.
Query multiple results or a
There are cases where the file header of file format A is "ff,11,22" and the file header of file format B is "ff,11,22,33", when you want to match the header of the file is "ff,11,22,33,44", then it will match up to both results, and return all the extensions they contain
1 // This returns when the first result is matched to a 2 // default is False 3 list<string> results = sniffer. Match (Filehead,false); 4 5 // This will match all the results. 6 list<string> results = sniffer. Match (Filehead,true);
Add a custom file header
You may have created a file format, or you are using a file format that is not in the metadata, and you can use the following code to add it to the Meta data:
1 vardata =New byte[]2 {3 0x11,4 0x22,5 0x336 };7Sniffer. ADD (data,New[] {" What","file","type"});
Note This newly added format has 3 extensions.
Get MimeType
1 list<string> result = sniffer. Match (head); 2 string mimeType = Mimetypes.getmimetype (result. First ());
Or
1 string mimeType = Mimetypes.getmimetype ("png");
In the next release, get MimeType will be replaced with a string extension method.
Meta data sources
Metadata from Wikipedia List of file signatures.
Roadmap
The next thing to do is to support a file format with offsets, such as
FF,one, ff start offset 10 bytes
And one , ff,??,??,??,,??, fd, a file format that contains jumps.
But the fact that they are two is a form that is expected to release support for the next release.
Support for. NET Standard. Now is. NET Core 2, which in the future will be adjusted to. NET Standard
GitHub Address: Https://github.com/rocketRobin/myrmec
NuGet Address: https://www.nuget.org/packages/Myrmec/
You are welcome to try, put forward comments and suggestions
Use Myrmec to detect the true format of a file in. netcore