Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Fine tuning file type detection
Message
From
15/06/2012 13:29:10
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Fine tuning file type detection
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01546098
Message ID:
01546098
Views:
83
I have this method to return the file type for some most common file types:
        ' Return the file type
        ' expC1 File name with the full path
        Public Function FileType(ByVal tcFile As String) As String
            Dim lcString As String = ""

            ' Read it as a binary
            lcString = FileToStrBinary(tcFile)

            ' If we have less than four characters
            If lcString.Length < 4 Then
                Return ""
            End If

            ' If this is a JPG
            If Left(lcString, 3) = Chr(255) + Chr(216) + Chr(255) Then
                Return "JPG"
            End If

            ' If this is a GIF
            If Left(lcString, 3) = "GIF" Then
                Return "GIF"
            End If

            ' If this is a EMF
            If Mid(lcString, 42, 3) = "EMF" Then
                Return "EMF"
            End If

            ' If this is a WMF
            If Left(lcString, 4) = Chr(215) + Chr(205) + Chr(198) + Chr(154) Then
                Return "WMF"
            End If

            ' If this is a TIF
            If Left(lcString, 4) = Chr(77) + Chr(77) + Chr(0) + Chr(42) Then
                Return "TIF"
            End If

            ' If this is a PNG
            If Left(lcString, 4) = Chr(137) + "PNG" Then
                Return "PNG"
            End If

            ' If this is a BMP
            If Left(lcString, 2) = "BM" Then
                Return "BMP"
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "CWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                Return "SWF"
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "FWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                Return "SWF"
            End If

            ' If this is a ZIP
            If Left(lcString, 4) = "PK" + Chr(3) + Chr(4) Then
                Return "ZIP"
            End If

            ' If this is a XML
            If UCase(Left(lcString, 6)) = "<?XML " Then
                Return "XML"
            End If

            ' If this is a DOC
            If Left(lcString, 8) = Chr(208) + Chr(207) + Chr(17) + Chr(224) + _
             Chr(161) + Chr(177) + Chr(26) + Chr(225) Then
                Return "DOC"
            End If

            ' If this is a PDF
            If UCase(Left(lcString, 5)) = "%PDF-" Then
                Return "PDF"
            End If

            Return ""
        End Function
I would like to fine tune it so it would also recognize DOCX and XLSX. Anyone knows the signature that would apply to those two?

I have found this page but would like a double confirmation:

http://www.garykessler.net/library/file_sigs.html
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform