代码如下:
Option Explicit
Public Function IsTextFile(Bytes() As Byte) As Boolean
Dim i As Long, AscN As Long, Length As Long
Length = UBound(Bytes) + 1
If Length < 3 Then
IsTextFile = IsGB(Bytes, Length)
Exit Function
ElseIf Bytes(0) = &HEF And Bytes(1) = &HBB And Bytes(2) = &HBF Then
IsTextFile = True
Exit Function
End If
Do While i <= Length - 1
If Bytes(i) < 128 Then
i = i + 1
AscN = AscN + 1
ElseIf (Bytes(i) And &HE0) = &HC0 And (Bytes(i + 1) And &HC0) = &H80 Then
i = i + 2
ElseIf i + 2 < Length Then
If (Bytes(i) And &HF0) = &HE0 And (Bytes(i + 1) And &HC0) = &H80 And (Bytes(i + 2) And &HC0) = &H80 Then
i = i + 3
Else
IsTextFile = IsGB(Bytes, Length)
Exit Function
End If
Else
IsTextFile = IsGB(Bytes, Length)
Exit Function
End If
Loop
If AscN = Length Then
IsTextFile = IsGB(Bytes, Length)
Else
IsTextFile = True
End If
End Function
Private Function IsGB(Bytes() As Byte, FileSize As Long) As Boolean
Dim ObjStream As Object, ret As String
Set ObjStream = CreateObject("ADODB.Stream")
With ObjStream
.Type = 1
.Mode = 3
.open
.Write Bytes
.Position = 0
.Type = 2
.Charset = "GB2312"
ret = .ReadText
.Close
End With
If LenB(StrConv(ret, vbFromUnicode)) = FileSize Then
IsGB = True
Else
IsGB = False
End If
End Function
摘自:网络整理
相关文章参考:
※VB计算文本文件的行数
★VB部分相关