前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA解析复合文档05——读取数据流

VBA解析复合文档05——读取数据流

作者头像
xyj
发布2020-08-10 09:46:21
7340
发布2020-08-10 09:46:21
举报
文章被收录于专栏:VBA 学习

根据前面解析得到的FAT数组、MiniFAT数组、目录信息,读取某个数据流就很简单了,只需要根据FAT数组或者MiniFAT数组构建的扇区链表,逐个扇区的去读取就可以:

代码语言:javascript
复制

'读取某个数据流
'dir_name   需要读取的文件名称,是复合文档中的文件名称
'RetBytes   返回解压后的数据Byte数组
'Return     返回出错信息
Function GetStream(dir_name As String, RetBytes() As Byte) As String
    If cf.h.Exists(dir_name) Then
        GetStream = GetStreamByDirIndex(VBA.CLng(cf.h.GetItem(dir_name)), RetBytes)
    Else
        GetStream = "复合文档:不存在的目录"
        Exit Function
    End If
End Function
'读取数据流
Private Function GetStreamByDirIndex(dirIndex As Long, RetBytes() As Byte) As String
    '1仓storage 2流 5根
    If cf.ArrDir(dirIndex).ObjectType <> 2 Then
        GetStreamByDirIndex = "复合文档:不是数据流"
        Exit Function
    End If
    If cf.ArrDir(dirIndex).StartingSectorID = Free_SID Then
        GetStreamByDirIndex = "复合文档:流的大小为0"
        Exit Function
    End If
    
    ReDim RetBytes(cf.ArrDir(dirIndex).StreamSize - 1) As Byte
    If cf.ArrDir(dirIndex).StreamSize < cf.Header.MiniStreamSize Then
        GetStreamByDirIndex = GetStreamMiniFAT(dirIndex, RetBytes)
    Else
        GetStreamByDirIndex = GetStreamFAT(dirIndex, RetBytes)
    End If
    
End Function

'按照FAT读取数据
Private Function GetStreamFAT(dirIndex As Long, RetBytes() As Byte) As String
    Dim b() As Byte
    ReDim b(cf.lSectorSize - 1) As Byte
    
    '找到读取开始的位置
    Dim sid As Long
    sid = cf.ArrDir(dirIndex).StartingSectorID
    
    Dim i As Long
    Dim p As Long
    
    Do
        '设置读取的位置
        cf.r.SeekFile getOffsetBySID(sid), OriginF
        
        cf.r.Read b
        For i = 0 To cf.lSectorSize - 1
            RetBytes(p) = b(i)
            p = p + 1
            If p = cf.ArrDir(dirIndex).StreamSize Then Exit Function
        Next
        
        '下一个扇区
        sid = cf.FAT(sid)
    Loop
    
End Function

'按照MiniFAT读取数据
Private Function GetStreamMiniFAT(dirIndex As Long, RetBytes() As Byte) As String
    Dim b() As Byte
    ReDim b(cf.lShortSectorSize - 1) As Byte
    
    '找到读取开始的位置
    Dim miniSID As Long
    miniSID = cf.ArrDir(dirIndex).StartingSectorID
    
    Dim i As Long
    Dim p As Long
    
    Do Until miniSID = End_Of_Chain_SID
        '设置读取的位置
        cf.r.SeekFile getOffsetByMiniFATSID(miniSID), OriginF
        
        cf.r.Read b
        For i = 0 To cf.lShortSectorSize - 1
            RetBytes(p) = b(i)
            p = p + 1
            If p = cf.ArrDir(dirIndex).StreamSize Then Exit Do
        Next
        
        '下一个扇区
        miniSID = cf.MiniFAT(miniSID)
    Loop
End Function
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 VBA 学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档