ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 832|回复: 0

[求助] Open获取文件二进制数据的问题(字符编码)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-10-5 13:00 | 显示全部楼层 |阅读模式
本帖最后由 laiwatch 于 2019-10-5 13:02 编辑
  1. Option Base 0

  2. Public Type MD5_CTX
  3.     z(1) As Long
  4.     Buf(3) As Long
  5.     inc(63) As Byte
  6.     digest(15) As Byte
  7. End Type

  8. Public Declare Sub MD5Init Lib "Cryptdll.dll" (ByVal pContex As Long)
  9. Public Declare Sub MD5Final Lib "Cryptdll.dll" (ByVal pContex As Long)
  10. Public Declare Sub MD5Update Lib "Cryptdll.dll" (ByVal pContex As Long, ByVal lPtr As Long, ByVal nSize As Long)

  11. Public Function ConvBytesToBinaryString(bytesIn() As Byte) As String
  12.     Dim z As Long
  13.     Dim nSize As Long
  14.     Dim strRet As String
  15.    
  16.     nSize = UBound(bytesIn)
  17.     For z = 0 To nSize
  18.          strRet = strRet & Right$("0" & Hex(bytesIn(z)), 2)
  19.     Next
  20.     ConvBytesToBinaryString = strRet
  21. End Function

  22. Public Function GetMD5Hash(bytesIn() As Byte) As Byte()
  23.     Dim ctx As MD5_CTX
  24.     Dim nSize As Long
  25.    
  26.     nSize = UBound(bytesIn) + 1
  27.    
  28.     MD5Init VarPtr(ctx)
  29.     MD5Update ByVal VarPtr(ctx), ByVal VarPtr(bytesIn(0)), nSize
  30.     MD5Final VarPtr(ctx)
  31.    
  32.     GetMD5Hash = ctx.digest
  33. End Function

  34. Public Function GetMD5Hash_Bytes(bytesIn() As Byte) As String
  35.     GetMD5Hash_Bytes = ConvBytesToBinaryString(GetMD5Hash(bytesIn))
  36. End Function

  37. Public Function GetMD5Hash_String(ByVal strIn As String) As String
  38.     GetMD5Hash_String = GetMD5Hash_Bytes(StrConv(strIn, vbFromUnicode))
  39. End Function

  40. Public Function GetMD5Hash_File(ByVal strFile As String) As String
  41.     Dim lFile As Long
  42.     Dim bytes() As Byte
  43.     Dim lSize As Long
  44. '    Dim Buf() As Byte
  45. '   Debug.Print InStr(strFile, "?")
  46. '    Dim sk As New ADODB.Stream
  47.    
  48. '    Dim iRe As New ADODB.Recordset
  49.   ' Cells(16, 3) = StrConv(strFile, vbNarrow)
  50.     lSize = fso.GetFile(strFile).Size
  51. '    strFilea = Replace(strFile, "?", ChrW(8226))
  52.     If (lSize) Then
  53.         lFile = FreeFile
  54.         ReDim bytes(lSize - 1)
  55. '        Debug.Print fso.GetAbsolutePathName(strFile)
  56.         Open strFile For Binary Access Read As lFile
  57. ''        With sk
  58. ''        .Type = adTypeBinary
  59. ''        .Open
  60. ''        .LoadFromFile (strFile)
  61. ''        End With
  62.         Get lFile, , bytes
  63. ''       Get sk, , bytes
  64.         Close lFile
  65. 'bytes = iRe
  66. 'k = lSize - 1
  67. 'iRe.Open
  68. 'ReDim arr(0 To lSize - 1)
  69. 'For I = 0 To iRe.RecordCount
  70. 'arr(I) = iRe
  71. 'iRe.MoveNext
  72. 'Next
  73. '
  74. 'iRe.Close
  75. 'sk.Close
  76. 't = Timer
  77. '  Dim I As Long
  78. '  DoEvents
  79. '  With CreateObject("ADODB.Stream")
  80. '    .Mode = 3: .Type = 1: .Open: .LoadFromFile strFile
  81. '    .Mode = 3
  82. '    .Type = adTypeBinary
  83. '    .Open
  84. '    .LoadFromFile (strFile)
  85. '    ReDim Buf(lSize - 1)
  86. '    For I = 0 To lSize - 1: Buf(I) = AscB(.Read(1)): Next
  87. '    For I = 0 To lSize - 1
  88. '    Buf(I) = AscB(.Read(1))
  89. 'Buf(I) = AscB(.Read(1))
  90. '    Next
  91. '    .Close
  92. '  End With
  93. '  MsgBox Format(Timer - t, "0.0000")
  94. 'ReadBinary = Buf
  95.         GetMD5Hash_File = GetMD5Hash_Bytes(bytes)
  96. 'GetMD5Hash_File = GetMD5Hash_Bytes(Buf)
  97.     End If
  98.    
  99. End Function

  100. Sub kjii()
  101. 'Dim arr(1) As String
  102. 'arr(1) = Range("e10").Value
  103. Cells(15, 3) = GetMD5Hash_File(Range("e10").Value)

  104. End Sub

  105. 'Function ReadBinary(FileName)
  106. '  Dim I As Long
  107. '  With CreateObject("ADODB.Stream")
  108. '    .Mode = 3: .Type = 1: .Open: .LoadFromFile FileName
  109. '    ReDim Buf(.Size - 1)
  110. '    For I = 0 To .Size - 1: Buf(I) = AscB(.Read(1)): Next
  111. '    .Close
  112. '  End With
  113. '  ReadBinary = Buf
  114. 'End Function
复制代码
在这句"Open strFile For Binary Access Read As lFile"这里,当strFile变量包含一些其他的字符时候,将会出现错误
如strFile="C:\Users\Master\Desktop\Åúá¿éú2ú1¤×÷2¾\[大家网]Python基础教程(第2版)[www.TopSage.com].pdf"就会出现"文件名或文件号错误"的问题
字符.zip (15.89 KB, 下载次数: 2)





您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-17 06:27 , Processed in 0.034682 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表