中易网

vb6如何用base64编码大文件

答案:2  悬赏:80  
解决时间 2021-03-06 08:40
vb6如何用base64编码大文件?我用vb6.0的base64编码来编码小文件,很快就可以编码出没错的编码,但是对于大于5M的文件,程序就会卡死了,不知道要怎么写才能使vb6.0的base64能够编码较大的二进制文件,请大家帮忙下,写出代码,谢谢了!
代码太长,没法贴出来呀,大家能不能帮我写出一个能够编码大小超过5M的二进制文件的base64代码呀~~~
最佳答案
'Base64编码函数,参数sFileName:与编码文件路径,sCodeName:编码文件存储路径
Function Base64Encode(ByVal sFileName As String, ByVal sCodeName As String) As String
Dim sCodeTable(0 To 63) As Byte '编码表
Dim tmpBytes3(1 To 3) As Byte '临时存储原字节码
Dim tmpBytes4(1 To 4) As Byte '临时存储字节分解吗

Dim i As Long, k As Long
Dim sFileLen As Long '文件长度
Dim n As Long
Dim m As Long

'初始化编码表
For i = 0 To 25
sCodeTable(i) = i + 65 '大写字母
sCodeTable(i + 26) = i + 97 '小写字母
Next
For i = 52 To 61
sCodeTable(i) = i - 4
Next
sCodeTable(62) = Asc("+")
sCodeTable(63) = Asc("/")

'文件长度
sFileLen = FileLen(sFileName)
n = sFileLen \ 3 '整除3
m = sFileLen Mod 3 '除以3的余数

'打开文件
Open sFileName For Binary As #1 Len = 32760
Open sCodeName For Binary As #2 Len = 32760

For i = 1 To n
Get #1, , tmpBytes3 '读取3个字节到tmpBytes
'八3个字节分解为4个字节
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
tmpBytes4(3) = (tmpBytes3(2) And 15) * 4 + (tmpBytes3(3) And 192) / 64 '截取第三个6bit
tmpBytes4(4) = tmpBytes3(3) And 63 '截取第三个6bit
For k = 1 To 4
tmpBytes4(k) = sCodeTable(tmpBytes4(k))
Next
Put #2, , tmpBytes4 '将编码写入编码文件

DoEvents '测试时使用,编译时可以注释掉===========================================
Next

'如果文件大小不是3的整数倍
If m = 1 Then
Get #1, , tmpBytes3(1)
tmpBytes3(2) = 0
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
Put #2, , sCodeTable(tmpBytes4(1))
Put #2, , sCodeTable(tmpBytes4(2))
Put #2, , CByte(Asc("="))
Put #2, , CByte(Asc("="))
ElseIf m = 2 Then
Get #1, , tmpBytes3(1)
Get #1, , tmpBytes3(2)
tmpBytes3(3) = 0
tmpBytes4(1) = (tmpBytes3(1) And 252) / 4 '截取前6bit
tmpBytes4(2) = (tmpBytes3(1) And 3) * 16 + (tmpBytes3(2) And 240) / 16 '截取第二个6bit
tmpBytes4(3) = (tmpBytes3(2) And 15) * 4 + (tmpBytes3(3) And 192) / 64 '截取第三个6bit
Put #2, , sCodeTable(tmpBytes4(1))
Put #2, , sCodeTable(tmpBytes4(2))
Put #2, , sCodeTable(tmpBytes4(3))
Put #2, , CByte(Asc("="))
End If
'关闭文件
Close #2
Close #1

End Function
全部回答
private declare function createstreamonhglobal lib "ole32.dll" ( _                           byval hglobal as long, _                           byval fdeleteonrelease as long, _                           lpistream as iunknown) as long private declare function oleloadpicture lib "oleaut32.dll" ( _                           byval lpstream as iunknown, _                           byval lsize as long, _                           byval frunmode as long, _                           riid as any, _                           lpipicture as ipicture) as long function bytetopict(picturedata() as byte) as stdpicture 'byte转图片     dim iid_ipicture(3) as long     dim opicture as ipicture     dim nresult as long     dim ostream as iunknown     dim hglobal as long     iid_ipicture(0) = &h7bf80980     iid_ipicture(1) = &h101abf32     iid_ipicture(2) = &haa00bb8b     iid_ipicture(3) = &hab0c3000     call createstreamonhglobal(varptr(picturedata(lbound(picturedata))), 0, ostream)     nresult = oleloadpicture(ostream, 0, 0, iid_ipicture(0), opicture)     if nresult = 0 then         set bytetopict = opicture     end if end function先把byte转为stdpicture再进行savepicture
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
卓研美容养生会馆地址在什么地方,想过去办事
鱼字旁 一个曲一个豆 读什么字
滚筒洗衣机洗完后,怎么开门
郝志鹏的人物介绍
山东省疾病预防控制中心 是不是正规的??可
时尚剪吧专业烫染工作室在什么地方啊,我要过
每次遇到贷款的骗子怎么办
璐璐天赐良鸡怎么去啊,有知道地址的么
歌词中带有“就让月亮见证我爱你有多深”
三亚鑫国盛石材在哪里啊,我有事要去这个地方
投诉信范本
地板里为什么有种黑色的小虫
变频器对小功率电机定子电流采集造成干扰,如
初干初地址在哪,我要去那里办事
平安驾校办理危险品从业资格证吗
推荐资讯
成都双流奥特莱斯有梅丽莎吗
西坊坞村地址在什么地方,想过去办事
23410算24三种方法
急求恢复手机照片的办法
阿米尼地址有知道的么?有点事想过去
oppo 和 bbk 是同一公司的?
潮南区两英镇墙新一卫生站我想知道这个在什么
6×8等于方块除以方块方块儿里填上合适的数
三式水听一定要配三式爆么
奇乐美蛋糕店地址有知道的么?有点事想过去
五十万韩元在韩国四天吃喝玩乐够用了吗
乔氏桌球会馆一部怎么去啊,有知道地址的么
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?