燈明ブログ

現状は小池啓仁の応援ブログ

ファイルのMimeを判断する

ファイルがテキストファイルなのか、バイナリファイルなのか…判断したい場合、FindMimeFromDataが使えます。
FindMimeFromDataは、データを解析し、以下のMimeを判断できるようです。

◆FindMimeFromDataが返すMimeリスト

MIME Type Description
text/plain Plain text. Default if data is primarily text and no other type detected.
text/html HTML. Default if common tags detected and server does not supply image/* type.
text/xml XML data. Default if data specifies
text/richtext Rich Text Format (RTF).
text/scriptlet Microsoft Windows script component.
audio/x-aiff Audio Interchange File, Macintosh.
audio/basic Audio file, UNIX.
audio/mid Internet Explorer 7 and later. MIDI sequence.
audio/wav Pulse Code Modulation (PCM) Wave audio, Windows.
image/gif Graphics Interchange Format (GIF).
image/jpeg JPEG image.
image/pjpeg Default type for JPEG images.
image/png Internet Explorer 7 and later. Portable Network Graphics (PNG).
image/x-png Internet Explorer 7 and later. Default type for PNG images.
image/tiff Tagged Image File Format (TIFF) image.
image/bmp Bitmap (BMP) image.
image/x-xbitmap Removed from Internet Explorer 8.
image/x-jg AOL Johnson-Grace compressed file.
image/x-emf Enhanced Metafile (EMF).
image/x-wmf Windows Metafile Format (WMF).
video/avi Audio-Video Interleaved (AVI) file.
video/mpeg MPEG stream file.
application/octet-stream Binary file. Default if data is primarily binary.
application/postscript PostScript (.ai, .eps, or .ps) file.
application/base64 Base64-encoded bytes.
application/macbinhex40 BinHex for Macintosh.
application/pdf Portable Document Format (PDF).
application/xml XML data. Must be server-supplied. See also "text/xml" type.
application/atom+xml Internet Explorer 7 and later. Atom Syndication Format feed.
application/rss+xml Internet Explorer 7 and later. Really Simple Syndication (RSS) feed.
application/x-compressed UNIX tar file, Gzipped.
application/x-zip-compressed Compressed archive file.
application/x-gzip-compressed Gzip compressed archive file.
application/java Java applet.
application/x-msdownload Executable (.exe or .dll) file.
http://msdn.microsoft.com/en-us/library/ms775147(VS.85).aspx

サンプルソース

<DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
Private Function FindMimeFromData( _
    ByVal pBC As IntPtr, _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal pwzUrl As String, _
    <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I1, SizeParamIndex:=3)> ByVal pBuffer As Byte(), _
    ByVal cbSize As Integer, _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal pwzMimeProposed As String, _
    ByVal dwMimeFlags As Integer, _
    ByRef ppwzMimeOut As IntPtr, _
    ByVal dwReserved As Integer) As Integer
End Function

Public Function getMimeFromFile(ByVal file As String) As String

    Dim mimeout As IntPtr

    If Not System.IO.File.Exists(file) Then
        Throw New FileNotFoundException()
    End If

    Dim MaxContent As Integer = CInt(New FileInfo(file).Length)

    If MaxContent > 256 Then MaxContent = 256

    Using fs As New FileStream(file, FileMode.Open)
        Dim buf(MaxContent) As Byte
        fs.Read(buf, 0, MaxContent)
        fs.Close()
        Dim result As Integer = FindMimeFromData(IntPtr.Zero, file, buf, MaxContent, Nothing, 0, mimeout, 0)
    End Using

    Dim mime As String = Marshal.PtrToStringUni(mimeout)

    Marshal.FreeCoTaskMem(mimeout)

    Return mime
End Function
http://dobon.net/cgi-bin/vbbbs/srch.cgi?PAGE=20&andor=and&logs=.%2Fvb%2Fvb.dat&no=0&word=intptr

◆補足

getMimeFromFile関数の引数へファイルパス名を指定します。リターン値は、MIME Typeです。


尚、マーシャル(Marshal)は、あるプログラムで利用されるデータ形式を、異なるプログラムで利用可能な形式に変換することです。


MarshalAsやMarshalでC言語のデータ形式VBで利用可能に変換しているようです。


◆留意点

ご自分が使うシステムで念入りに検証してから適用した方が良いでしょう!