VBA解析复合文档08——应用-解析Thumbs.db

时间:2022-07-23
本文章向大家介绍VBA解析复合文档08——应用-解析Thumbs.db,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在前面提到过,Thumbs.db文件也是一个复合文档,里面存储的数据流就是图片的缩略图。

找到一个有图片的文件夹,不管看没看到Thumbs.db这个文件,替换下面代码中的路径:

Private Sub Test_ParseThumbs()
    Dim f As CFile
    
    Set f = NewCFile()
    f.OpenFile "C:UsersAdministratorPicturesdyhThumbs.db"
    
    Dim cf As CCompoundFile
    
    Set cf = New CCompoundFile
    
    Dim ret As String
    ret = cf.Parse(f)
    If VBA.Len(ret) Then
        Debug.Print ret
        Exit Sub
    End If
    
    Dim fs() As String
    fs = cf.DirsName()
    
    Dim b() As Byte, bpic() As Byte
    Dim i As Long, j As Long
    For i = 0 To UBound(fs)
        If fs(i) <> "Root Entry" Then
            ret = cf.GetStream(fs(i), b)
            If VBA.Len(ret) Then
                Debug.Print ret
                Exit Sub
            End If
            
            '保存
    '        每一个缩略图IStream的前3个整形不是缩略图的内容,64位电脑就是24个Byte
            ReDim bpic(UBound(b) - 24) As Byte
            For j = 24 To UBound(b)
                bpic(j - 24) = b(j)
            Next
            
            ByteToFile "C:UsersAdministratorPicturesdyhVBAParse" & fs(i) & ".jpg", bpic
        End If
    Next
    
    
    Set cf = Nothing
    Set f = Nothing
End Sub

Function ByteToFile(file_name As String, b() As Byte)
    Dim iFreefile As Integer
    
    iFreefile = VBA.FreeFile()
    Open file_name For Binary As iFreefile
    Put #iFreefile, 1, b
    Close iFreefile
End Function

注意自己电脑的系统版本,我的电脑是64位的,所以数据流前面3个整形是3×8=24个Byte,如果是32位的,应该是3×4=12个Byte,这个不是缩略图的数据,需要跳过。

实现效果: