有效地将二进制数据嵌入脚本中 [英] Embedding binary data in a script efficiently

查看:78
本文介绍了有效地将二进制数据嵌入脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些用于类Unix系统的安装文件(例如,对于Matlab或Mathematica来说是巨大的install.sh),它们必须嵌入很多二进制数据,例如图标,声音,图形等. ,放入脚本中.我想知道如何做到这一点,因为这在简化文件结构方面可能很有用.

I have seen some installation files (huge ones, install.sh for Matlab or Mathematica, for example) for Unix-like systems, they must have embedded quite a lot of binary data, such as icons, sound, graphics, etc, into the script. I am wondering how that can be done, since this can be potentially useful in simplifying file structure.

我对使用Python和/或Bash做到这一点特别感兴趣.

I am particularly interested in doing this with Python and/or Bash.

我在Python中了解的现有方法:

Existing methods that I know of in Python:

  1. 只需使用一个字节字符串:x = b'\x23\xa3\xef' ...,效率很低,对于100KB wav文件来说要占用一半的内存.
  2. base64比选项1更好,将大小增加了4/3倍.
  1. Just use a byte string: x = b'\x23\xa3\xef' ..., terribly inefficient, takes half a MB for a 100KB wav file.
  2. base64, better than option 1, enlarge the size by a factor of 4/3.

我想知道是否还有其他(更好)的方法?

I am wondering if there are other (better) ways to do this?

推荐答案

您可以使用base64 +压缩(使用

You can use base64 + compression (using bz2 for instance) if that suits your data (e.g., if you're not embedding already compressed data).

例如,要创建您的数据(例如,您的数据由100个空字节组成,后跟200个字节,值0x01):

For instance, to create your data (say your data consist of 100 null bytes followed by 200 bytes with value 0x01):

>>> import bz2
>>> bz2.compress(b'\x00' * 100 + b'\x01' * 200).encode('base64').replace('\n', '')
'QlpoOTFBWSZTWcl9Q1UAAABBBGAAQAAEACAAIZpoM00SrccXckU4UJDJfUNV'

并使用它(在您的脚本中)将数据写入文件:

And to use it (in your script) to write the data to a file:

import bz2
data = 'QlpoOTFBWSZTWcl9Q1UAAABBBGAAQAAEACAAIZpoM00SrccXckU4UJDJfUNV'
with open('/tmp/testfile', 'w') as fdesc:
    fdesc.write(bz2.decompress(data.decode('base64')))

这篇关于有效地将二进制数据嵌入脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆