在elisp中读取二进制数据

时间:2012-08-16 15:57:30

标签: emacs elisp

我正在尝试通过elisp中的XML-RPC从文件(PNG图像)传输一大块二进制数据。这是自动将附件上传到Confluence wiki的一部分,特别是当前页面中使用的本地图像。 执行此操作的代码是:

        ;; Open the file and get content
        (with-temp-buffer
          ;; file-name is the PNG file name, which is binary data
          (find-file (expand-file-name file-name current-dir))
          ;; Setup Confluence request alist
          (setq confl-req (list 
                           (cons "fileName" file-name)
                           (cons "contentType" mime-type)))
          ;; RPC call
          (setq confl-reply (cfln-rpc-execute 'confluence1.addAttachment
                                              page-id confl-req (buffer-substring-no-properties (point-min) (point-max))))

我对作品(buffer-substring-no-properties (point-min) (point-max))有疑问。上传到Confluence的二进制数据与几个位置的PNG文件不匹配。我注意到附加文件中的0xe0 0x88字节被替换为0xc8。知道如何获取文件中包含的确切二进制数据吗?

谢谢, NMA

2 个答案:

答案 0 :(得分:3)

您应该使用insert-file-contents-literally代替find-file

(insert-file-contents-literally FILENAME &optional VISIT BEG END
REPLACE)

Like `insert-file-contents', but only reads in the file literally.
A buffer may be modified in several ways after reading into the buffer,
to Emacs features such as format decoding, character code
conversion, `find-file-hook', automatic uncompression, etc.

This function ensures that none of these modifications will take place.

答案 1 :(得分:0)

f-library提供了@npostavs答案中的一站式解决方案: https://github.com/rejeep/f.el

f-read-bytes

它使用

insert-file-contents-literally

一起
buffer-substring-no-properties

负责编码和多字节处理......