;; base64 编码与解码通用函数 by. yxp 2017-02-15
;;功能: base64 编码。将一个任意文件转换为 base64 码
;;参数: file 需要编码的文件全路径
;;变量: 成功返回 txt 变量,输出 file 文件的 base64 编码
;;返回: 成功则返回 0,失败返回 nil
;;示例: (enbase64 "d:\\aa.bin" "d:\\aa.txt")
;;;(enbase64 "C:\\Users\\Admin\\Desktop\\BJPXGJ.slb" "C:\\Users\\Admin\\Desktop\\BJPXGJ.txt")
;;说明: base64 编码。将一个任意文件转换为 base64 码
;;参数:file:需要编码的文件完整路径
;;参数:sfile:文件输出保存完整路径
;;返回:成功则返回 T,失败返回 nil
(defun enbase64(file sfile / bf-str->lst bin f msx msxb rado str strlst)
(setq time0 (getvar "date"))
(setq
rado (vlax-create-object "adodb.stream")
msx (vlax-create-object "msxml2.domdocument")
file (findfile file)
)
(if (and rado msx file)
(progn
(defun BF-str->lst (str del / pos)
(if (setq pos (vl-string-search del str))
(cons (substr str 1 pos)
(BF-str->lst (substr str (+ pos 1 (strlen del))) del)
)
(list str)
)
)
(vlax-put-property rado 'type 1)
(vlax-invoke-method rado 'open 'nil 'nil 'nil 'nil 'nil)
(vlax-invoke-method rado 'loadfromfile file)
(setq bin (vlax-invoke-method rado 'read 'nil))
(vlax-invoke rado 'close)
(vlax-release-object rado) ;;释放对象
(setq msx (vlax-create-object "msxml2.domdocument"))
(setq msxb (vlax-invoke-method msx 'createelement "test"))
(vlax-put-property msxb 'datatype "bin.base64")
(vlax-put-property msxb 'nodetypedvalue bin)
(setq str (vlax-get-property msxb 'text))
(vlax-release-object msx)
(setq strlst (BF-str->lst str "\n"))
(setq f (open sfile "w"))
(write-line "(setq strlist" f)
(write-line " '(" f)
(foreach x strlst (write-line (strcat " " (vl-prin1-to-string x)) f))
(write-line " )" f)
(write-line ")" f)
(close f)
(setq time1 (getvar "date"))
(princ (strcat "\nBase64编码耗时: " (rtos (* 86400 (- time1 time0)) 2 4) " 秒\n"))
T
)
nil
)
)
;;功能: base64 解码。base64 将文本转换为需要的二进制文件
;;参数: strlst - base64 码的字符串列表; file - 保存文件的全路径
;;返回: 成功则将 str 保存到 file,并返回 t,失败返回 nil
;;示例:(setq strlst '( "QXV0b0NBRCBTbGlkZSBMaWJyYXJ5IDEuMA0KGgAARgBJTUcxUjFDAAAAAAAAAAAAAAAAAAAA"
;; "AAAAAAAAAAAAALAAAABJTUcxUjJDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN2TAQBJTUcx"
;; "UjNDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKYCAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
;; "AAAAAAAAAAAAACtwBABBdXRvQ0FEIFNsaWRlDQoaAFYCnwKhAmoimAACADQSAv8C/wD9BAD+"
;; )
;; )
;; (unbase64 strlst (strcat (Vla-Get-Path (Vlax-Get-Acad-Object)) "\\Support\\BJPXGJ.slb"))
(defun unbase64(strlst file / bin msx msxb wado)
(setq
wado (vlax-create-object "adodb.stream")
msx (vlax-create-object "msxml2.domdocument")
)
(if(and wado msx)
(progn
(setq msxb (vlax-invoke-method msx 'createelement "test"))
(vlax-put-property msxb 'datatype "bin.base64")
(setq str (apply 'strcat strlst))
(vlax-put-property msxb 'text str)
(setq bin (vlax-get-property msxb 'nodetypedvalue))
(vlax-release-object msx)
(vlax-put-property wado 'type 1)
(vlax-invoke-method wado 'open 'nil 'nil 'nil 'nil 'nil)
(vlax-invoke-method wado 'write bin)
(vlax-invoke-method wado 'savetofile file 2)
(vlax-invoke-method wado 'close)
(vlax-release-object wado)
T
)
nil
)
)
修改了下,输出文件就是这个样子