vb6如何用base64编码大文件
vb6如何用base64编码大文件?我用vb6.0的base64编码来编码小文件,很快就可以编码出没错的编码,但是对于大于5M的文件,程序就会卡死了,不知道要怎么写才能使...
vb6如何用base64编码大文件?我用vb6.0的base64编码来编码小文件,很快就可以编码出没错的编码,但是对于大于5M的文件,程序就会卡死了,不知道要怎么写才能使vb6.0的base64能够编码较大的二进制文件,请大家帮忙下,写出代码,谢谢了!
代码太长,没法贴出来呀,大家能不能帮我写出一个能够编码大小超过5M的二进制文件的base64代码呀~~~ 展开
代码太长,没法贴出来呀,大家能不能帮我写出一个能够编码大小超过5M的二进制文件的base64代码呀~~~ 展开
2个回答
展开全部
'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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询