1. ;; base64 编码与解码通用函数 by. yxp 2017-02-15
    2. ;;功能: base64 编码。将一个任意文件转换为 base64
    3. ;;参数: file 需要编码的文件全路径
    4. ;;变量: 成功返回 txt 变量,输出 file 文件的 base64 编码
    5. ;;返回: 成功则返回 0,失败返回 nil
    6. ;;示例: (enbase64 "d:\\aa.bin" "d:\\aa.txt")
    7. ;;;(enbase64 "C:\\Users\\Admin\\Desktop\\BJPXGJ.slb" "C:\\Users\\Admin\\Desktop\\BJPXGJ.txt")
    8. ;;说明: base64 编码。将一个任意文件转换为 base64
    9. ;;参数:file:需要编码的文件完整路径
    10. ;;参数:sfile:文件输出保存完整路径
    11. ;;返回:成功则返回 T,失败返回 nil
    12. (defun enbase64(file sfile / bf-str->lst bin f msx msxb rado str strlst)
    13. (setq time0 (getvar "date"))
    14. (setq
    15. rado (vlax-create-object "adodb.stream")
    16. msx (vlax-create-object "msxml2.domdocument")
    17. file (findfile file)
    18. )
    19. (if (and rado msx file)
    20. (progn
    21. (defun BF-str->lst (str del / pos)
    22. (if (setq pos (vl-string-search del str))
    23. (cons (substr str 1 pos)
    24. (BF-str->lst (substr str (+ pos 1 (strlen del))) del)
    25. )
    26. (list str)
    27. )
    28. )
    29. (vlax-put-property rado 'type 1)
    30. (vlax-invoke-method rado 'open 'nil 'nil 'nil 'nil 'nil)
    31. (vlax-invoke-method rado 'loadfromfile file)
    32. (setq bin (vlax-invoke-method rado 'read 'nil))
    33. (vlax-invoke rado 'close)
    34. (vlax-release-object rado) ;;释放对象
    35. (setq msx (vlax-create-object "msxml2.domdocument"))
    36. (setq msxb (vlax-invoke-method msx 'createelement "test"))
    37. (vlax-put-property msxb 'datatype "bin.base64")
    38. (vlax-put-property msxb 'nodetypedvalue bin)
    39. (setq str (vlax-get-property msxb 'text))
    40. (vlax-release-object msx)
    41. (setq strlst (BF-str->lst str "\n"))
    42. (setq f (open sfile "w"))
    43. (write-line "(setq strlist" f)
    44. (write-line " '(" f)
    45. (foreach x strlst (write-line (strcat " " (vl-prin1-to-string x)) f))
    46. (write-line " )" f)
    47. (write-line ")" f)
    48. (close f)
    49. (setq time1 (getvar "date"))
    50. (princ (strcat "\nBase64编码耗时: " (rtos (* 86400 (- time1 time0)) 2 4) " \n"))
    51. T
    52. )
    53. nil
    54. )
    55. )
    56. ;;功能: base64 解码。base64 将文本转换为需要的二进制文件
    57. ;;参数: strlst - base64 码的字符串列表; file - 保存文件的全路径
    58. ;;返回: 成功则将 str 保存到 file,并返回 t,失败返回 nil
    59. ;;示例:(setq strlst '( "QXV0b0NBRCBTbGlkZSBMaWJyYXJ5IDEuMA0KGgAARgBJTUcxUjFDAAAAAAAAAAAAAAAAAAAA"
    60. ;; "AAAAAAAAAAAAALAAAABJTUcxUjJDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN2TAQBJTUcx"
    61. ;; "UjNDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKYCAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    62. ;; "AAAAAAAAAAAAACtwBABBdXRvQ0FEIFNsaWRlDQoaAFYCnwKhAmoimAACADQSAv8C/wD9BAD+"
    63. ;; )
    64. ;; )
    65. ;; (unbase64 strlst (strcat (Vla-Get-Path (Vlax-Get-Acad-Object)) "\\Support\\BJPXGJ.slb"))
    66. (defun unbase64(strlst file / bin msx msxb wado)
    67. (setq
    68. wado (vlax-create-object "adodb.stream")
    69. msx (vlax-create-object "msxml2.domdocument")
    70. )
    71. (if(and wado msx)
    72. (progn
    73. (setq msxb (vlax-invoke-method msx 'createelement "test"))
    74. (vlax-put-property msxb 'datatype "bin.base64")
    75. (setq str (apply 'strcat strlst))
    76. (vlax-put-property msxb 'text str)
    77. (setq bin (vlax-get-property msxb 'nodetypedvalue))
    78. (vlax-release-object msx)
    79. (vlax-put-property wado 'type 1)
    80. (vlax-invoke-method wado 'open 'nil 'nil 'nil 'nil 'nil)
    81. (vlax-invoke-method wado 'write bin)
    82. (vlax-invoke-method wado 'savetofile file 2)
    83. (vlax-invoke-method wado 'close)
    84. (vlax-release-object wado)
    85. T
    86. )
    87. nil
    88. )
    89. )

    修改了下,输出文件就是这个样子
    %VA[YW}86C`9BX9EP}N61]X.png