Sarvesh Dalvi
Sarvesh Dalvi

Reputation: 2688

How to get the extension of file in Flutter?

My goal is to identify the extension of a file through code like whether a given file is a .doc,.pdf,.svg etc.

One thing is to get it through the extension present at the end of the file's name. But what if the extension is not present in the file's name?

Another thing I tried is to determine it through initial bytes of the file. eg: .docx extension file starts with : [80,75,3,4,20,0,6, .....] In this case, I only know some of them. So where can I find initial bytes of all types of extensions ?

I think the second process is a valid way to do it but is there any other good way to determine the extension of a file ?

Upvotes: 1

Views: 1422

Answers (1)

Uroš
Uroš

Reputation: 1658

So where can I find initial bytes of all types of extensions ?

You can start with this these two:

  1. https://en.wikipedia.org/wiki/List_of_file_signatures
  2. https://www.garykessler.net/library/file_sigs.html

But there are many, many file types out there... it is quite possible you will never be able to find them all.

I would only use "Magic Bytes" when absolutely necessary , like salvaging data from a poorly thought out db that has blobs and doesn't keep track of what kind of file is in each blob... because at the end of the day it is still a hacky way to get the job done. If possible just reject file types without proper extensions.

Upvotes: 1

Related Questions