Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract file extension from byte array

Tags:

I've got bytes array in database.

How to extract file extension (mime/type) from byte array in java?

like image 299
emilan Avatar asked Apr 06 '12 07:04

emilan


People also ask

Can we convert byte array to file in Java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.

What is stored in a byte array?

A byte is 8 bits (binary data). A byte array is an array of bytes (tautology FTW!). You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.

How do I print a Bytearray?

You can simply iterate the byte array and print the byte using System. out. println() method.

Can we convert string to byte array in Java?

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.


2 Answers

It turned out that there is a decent method in JDK's URLConnection class, please refer to the following answer: Getting A File's Mime Type In Java

If one needs to extract file extension from byte array instead of file, one should simply use java.io.ByteArrayInputStream (class to read bytes specifically from byte arrays) instead of java.io.FileInputStream (class to read bytes specifically from files) like in the following example:

byte[] content = ; InputStream is = new ByteArrayInputStream(content); String mimeType = URLConnection.guessContentTypeFromStream(is);  //...close stream 

Hope this helps...

like image 105
Yuriy Nakonechnyy Avatar answered Sep 18 '22 20:09

Yuriy Nakonechnyy


If this is for storing a file that is uploaded:

  • create a column for the filename extension
  • create a column for the mime type as sent by the browser

If you don't have the original file, and you only have bytes, you have a couple of good solutions.

If you're able to use a library, look at using mime-util to inspect the bytes:

http://technopaper.blogspot.com/2009/03/identifying-mime-using-mime-util.html

If you have to build your own byte detector, here are many of the most popular starting bytes:

"BC" => bitcode, "BM" => bitmap, "BZ" => bzip, "MZ" => exe, "SIMPLE"=> fits, "GIF8" => gif, "GKSM" => gks, [0x01,0xDA].pack('c*') => iris_rgb, [0xF1,0x00,0x40,0xBB].pack('c*') => itc, [0xFF,0xD8].pack('c*') => jpeg, "IIN1" => niff, "MThd" => midi, "%PDF" => pdf, "VIEW" => pm, [0x89].pack('c*') + "PNG" => png, "%!" => postscript, "Y" + [0xA6].pack('c*') + "j" + [0x95].pack('c*') => sun_rasterfile, "MM*" + [0x00].pack('c*') => tiff, "II*" + [0x00].pack('c*') => tiff, "gimp xcf" => gimp_xcf, "#FIG" => xfig, "/* XPM */" => xpm, [0x23,0x21].pack('c*') => shebang, [0x1F,0x9D].pack('c*') => compress, [0x1F,0x8B].pack('c*') => gzip, "PK" + [0x03,0x04].pack('c*') => pkzip, "MZ" => dos_os2_windows_executable, ".ELF" => unix_elf, [0x99,0x00].pack('c*') => pgp_public_ring, [0x95,0x01].pack('c*') => pgp_security_ring, [0x95,0x00].pack('c*') => pgp_security_ring, [0xA6,0x00].pack('c*') => pgp_encrypted_data, [0xD0,0xCF,0x11,0xE0].pack('c*') => docfile 
like image 22
joelparkerhenderson Avatar answered Sep 17 '22 20:09

joelparkerhenderson