[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
参考:
VBS 二进制文件 与 Base64编码文本文件 互转工具
  1. Option Explicit
  2. Dim sTitle
  3. sTitle = "二进制文件 与 Base64编码文本文件 互转工具"
  4. If WScript.Arguments.Count = 0 Then
  5. MsgBox "使用方法:请将要格式化的文件拖动到这个文件上。", vbInformation, sTitle
  6. WScript.Quit
  7. End If
  8. Dim sReturn
  9. sReturn = Msgbox("请选择转化模式:" & vbCrLf & vbCrLf & _
  10.                 "   [ 是(Y) ]   二进制文件 转 Base64编码文本文件" & vbCrLf & _
  11.                 "   [ 否(N) ]   Base64编码文本文件 转 二进制文件 " & vbCrLf , _
  12.                 vbQuestion + vbYesNoCancel, sTitle)
  13.                
  14. If sReturn = vbCancel Then WScript.Quit
  15. Dim fso, wso
  16. Set fso = Createobject("Scripting.FileSystemObject")
  17. Set wso = CreateObject("WScript.Shell")
  18. Dim i
  19. For Each i In WScript.Arguments
  20.     If sReturn = vbYes Then BinaryToBase64 i, i & ".base64.txt"
  21.     If sReturn = vbNo  Then Base64ToBinary i, i & ".bin"
  22. Next
  23. wso.popup chr(13) + "文件格式化完成。    " + chr(13), 1, "提示", vbInformation
  24. WScript.Quit
  25. Function BinaryToBase64(ByVal BinaryFile, ByVal TextFile)
  26.     ' This script reads jpg picture named SuperPicture.jpg, converts it to base64
  27.     ' code using encoding abilities of MSXml2.DOMDocument object and saves
  28.     ' the resulting data to encoded.txt file
  29. ' http://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript
  30.     'Option Explicit
  31.     Const fsDoOverwrite     = true  ' Overwrite file with base64 code
  32.     Const fsAsASCII         = false ' Create base64 code file as ASCII file
  33.     Const adTypeBinary      = 1     ' Binary file is encoded
  34.     ' Variables for writing base64 code to file
  35.     Dim objFSO
  36.     Dim objFileOut
  37.     ' Variables for encoding
  38.     Dim objXML
  39.     Dim objDocElem
  40.     ' Variable for reading binary picture
  41.     Dim objStream
  42.     ' Open data stream from picture
  43.     Set objStream = CreateObject("ADODB.Stream")
  44.     objStream.Type = adTypeBinary
  45.     objStream.Open()
  46.     'objStream.LoadFromFile("SuperPicture.jpg")
  47.     objStream.LoadFromFile(BinaryFile)
  48.     ' Create XML Document object and root node
  49.     ' that will contain the data
  50.     Set objXML = CreateObject("MSXml2.DOMDocument")
  51.     Set objDocElem = objXML.createElement("Base64Data")
  52.     objDocElem.dataType = "bin.base64"
  53.     ' Set binary value
  54.     objDocElem.nodeTypedValue = objStream.Read()
  55.     ' Open data stream to base64 code file
  56.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  57.     'Set objFileOut = objFSO.CreateTextFile("encoded.txt", fsDoOverwrite, fsAsASCII)
  58.     Set objFileOut = objFSO.CreateTextFile(TextFile, fsDoOverwrite, fsAsASCII)
  59.     ' Get base64 value and write to file
  60.     objFileOut.Write objDocElem.text
  61.     objFileOut.Close()
  62.     ' Clean all
  63.     Set objFSO = Nothing
  64.     Set objFileOut = Nothing
  65.     Set objXML = Nothing
  66.     Set objDocElem = Nothing
  67.     Set objStream = Nothing
  68.    
  69. End Function
  70. Function Base64ToBinary(ByVal sTextFile, ByVal sBinaryFile)
  71.     Const foForReading          = 1 ' Open base 64 code file for reading
  72.     Const foAsASCII             = 0 ' Open base 64 code file as ASCII file
  73.     Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
  74.     Const adTypeBinary          = 1 ' Binary file is encoded
  75.     ' Variables for reading base64 code from file
  76.     Dim objFSO
  77.     Dim objFileIn
  78.     Dim objStreamIn
  79.     ' Variables for decoding
  80.     Dim objXML
  81.     Dim objDocElem
  82.     ' Variable for write binary picture
  83.     Dim objStream
  84.     ' Open data stream from base64 code filr
  85.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  86.     'Set objFileIn   = objFSO.GetFile("encoded.txt")
  87.     Set objFileIn   = objFSO.GetFile(sTextFile)
  88.     Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)
  89.     ' Create XML Document object and root node
  90.     ' that will contain the data
  91.     Set objXML = CreateObject("MSXml2.DOMDocument")
  92.     Set objDocElem = objXML.createElement("Base64Data")
  93.     objDocElem.DataType = "bin.base64"
  94.     ' Set text value
  95.     objDocElem.text = objStreamIn.ReadAll()
  96.     ' Open data stream to picture file
  97.     Set objStream = CreateObject("ADODB.Stream")
  98.     objStream.Type = adTypeBinary
  99.     objStream.Open()
  100.     ' Get binary value and write to file
  101.     objStream.Write objDocElem.NodeTypedValue
  102.     'objStream.SaveToFile "SuperPicture.jpg", adSaveCreateOverWrite
  103.     objStream.SaveToFile sBinaryFile, adSaveCreateOverWrite
  104.     ' Clean all
  105.     Set objFSO = Nothing
  106.     Set objFileIn = Nothing
  107.     Set objStreamIn = Nothing
  108.     Set objXML = Nothing
  109.     Set objDocElem = Nothing
  110.     Set objStream = Nothing
  111.    
  112. End Function
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表