0

I was given a task to convert binary data to file using c# and I am converting that using

Convert binary data to a pdf file

but a question came to my mind that how can we detect that what extension of file it is? Because in our database there is no column for extension.

Please give your suggestion?

Community
  • 1
  • 1
  • 1
    You could look into how the `file` utility reads some magic bytes and then guesses the file extension. Most files start with a specific set of bytes to tell their type (e.g. `GIF89a` for a gif graphic, or `BZh` for a bzip2 file) – knittl Apr 14 '16 at 05:32
  • @knittl: file starts with "UEsDBBQAAAAIAAdUhkjaV8dk6QQAADoJAAAMAAAAMDAwMDAwMDIuVEFYnZZPbNtUHMdtxy9O3SXt" – Gourav khanna Apr 14 '16 at 06:11
  • since it's only printable characters, it looks very much like a base64 encoded representation of the file's contents. How was the file inserted into the DB? – knittl Apr 14 '16 at 06:13
  • I don't know about how the file is inserted in it..:( – Gourav khanna Apr 14 '16 at 06:15
  • Well, it still looks like base64 data. Any chance some of the values end with one or two `=` characters? Running the start of your sample data through a base64 decoder results in `PK\x03\x04`, hinting at a zip file. – knittl Apr 14 '16 at 06:24
  • @Gkhanna File extension says nothing. This is just a hint to the operating system how to open the file. What interests you is what encoding / format of the file. To know that you need to know which formats can have in the DB, and then check the current file formats are suitable according to the file header. – Dudi Keleti Apr 14 '16 at 07:43

0 Answers0