vb6如何用base64编码大文件?我用vb6.0的base64编码来编码小文件,很快就可以编码出没错的编码,但是对于大于5M的文件,程序就会卡死了,不知道要怎么写才能使vb6.0的base64能够编码较大的二进制文件,请大家帮忙下,写出代码,谢谢了!
代码太长,没法贴出来呀,大家能不能帮我写出一个能够编码大小超过5M的二进制文件的base64代码呀~~~
vb6如何用base64编码大文件
答案:2 悬赏:80
解决时间 2021-03-06 08:40
- 提问者网友:控制庸俗
- 2021-03-06 05:35
最佳答案
- 二级知识专家网友:湫止没有不同
- 2021-03-06 07:06
'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
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
全部回答
- 1楼网友:迷人小乖乖
- 2021-03-06 07:16
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
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯