- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经阅读了 base64 python 文档并在 SO 和其他地方看到了示例,但是我在将 base64 解码回原始二进制表示时仍然遇到问题。
我没有收到任何异常,所以我认为不存在填充或字符集问题。我只是得到一个比原始二进制文件小的二进制文件。
我包括了 base64 编码和解码步骤,以防其中一个或两个步骤出现问题。
代码必须使用 python 2.7 运行。
下面是重现问题的脚本。
b64_encode.py
#!/usr/bin/env python2.7
#
# b64_encode.py - must run with python 2.7
# - must process data in chunks to limit memory consumption
# - base64 data must be JSON compatible, i.e.
# use base64 "modern" interface,
# not base64.encodestring() which contains linefeeds
#
import sys, base64
def write_base64_file_from_file(src_fname, b64_fname, chunk_size=8192):
with open(src_fname, 'rb') as fin, open(b64_fname, 'w') as fout:
while True:
bin_data = fin.read(chunk_size)
if not bin_data:
break
print 'bin %s data len: %d' % (type(bin_data), len(bin_data))
b64_data = base64.b64encode(bin_data)
print 'b64 %s data len: %d' % (type(b64_data), len(b64_data))
fout.write(b64_data)
if len(sys.argv) != 2:
print 'usage: %s <bin_fname>' % sys.argv[0]
sys.exit()
bin_fname = sys.argv[1]
b64_fname = bin_fname + '.b64'
write_base64_file_from_file(bin_fname, b64_fname)
b64_decode.py
#!/usr/bin/env python2.7
#
# b64_decode.py - must run with python 2.7
# - must process data in chunks to limit memory consumption
#
import os, sys, base64
def write_file_from_base64_file(b64_fname, dst_fname, chunk_size=8192):
with open(b64_fname, 'r') as fin, open(dst_fname, 'wb') as fout:
while True:
b64_data = fin.read(chunk_size)
if not b64_data:
break
print 'b64 %s data len: %d' % (type(b64_data), len(b64_data))
bin_data = base64.b64decode(b64_data)
print 'bin %s data len: %d' % (type(bin_data), len(bin_data))
fout.write(bin_data)
if len(sys.argv) != 2:
print 'usage: %s <b64_fname>' % sys.argv[0]
sys.exit()
b64_fname = sys.argv[1]
bin_ext = os.path.splitext(os.path.splitext(b64_fname)[0])[1]
bin_fname = os.path.splitext(b64_fname)[0] + bin_ext
write_file_from_base64_file(b64_fname, bin_fname)
例如,我对 19k 图像文件的输出是:
$ ./b64_encode.py img.jpg
bin <type 'str'> data len: 8192
b64 <type 'str'> data len: 10924
bin <type 'str'> data len: 8192
b64 <type 'str'> data len: 10924
bin <type 'str'> data len: 2842
b64 <type 'str'> data len: 3792
$ ./b64_decode.py img.jpg.b64
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 6144
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 2048
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 4097
b64 <type 'str'> data len: 1064
bin <type 'str'> data len: 796
$ ll
19226 Feb 5 14:24 img.jpg
25640 Mar 29 12:12 img.jpg.b64
13085 Mar 29 12:14 img.jpg.jpg
最佳答案
您确实遇到了填充问题:
>>> open('pianoavatar.jpg').read(8192).encode('base64')[-5:]
'IIE=\n'
当遇到 =
填充标记时,Base64 解码停止。您的第二次阅读在第 10924 个字符处找到了这样的标记。
您需要将 block 大小调整为可被 3 整除,以避免在输出文件中间出现填充。例如,使用 8190 的 block 大小。
读取时,您需要使用 4 的倍数的缓冲区大小,以避免遇到对齐问题。 8192
在那里会很好,但您必须确保在您的函数中满足此限制。您最好将输入 block 默认为 base64 扩展 block 大小; 10920 编码 block 大小为 8190(每 3 个字节编码 4 个 base64 字符)。
演示:
>>> write_base64_file_from_file('pianoavatar.jpg', 'test.b64', 8190)
bin <type 'str'> data len: 8190
b64 <type 'str'> data len: 10920
bin <type 'str'> data len: 8190
b64 <type 'str'> data len: 10920
bin <type 'str'> data len: 1976
b64 <type 'str'> data len: 2636
现在阅读工作正常,即使您的原始 block 大小为 8192:
>>> write_file_from_base64_file('test.b64', 'test.jpg', 8192)
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 6144
b64 <type 'str'> data len: 8192
bin <type 'str'> data len: 6144
b64 <type 'str'> data len: 8092
bin <type 'str'> data len: 6068
您可以使用简单的模数强制缓冲区大小在您的函数中对齐:
def write_base64_file_from_file(src_fname, b64_fname, chunk_size=8190):
chunk_size -= chunk_size % 3 # align to multiples of 3
# ...
def write_file_from_base64_file(b64_fname, dst_fname, chunk_size=10920):
chunk_size -= chunk_size % 4 # align to multiples of 4
# ...
关于python - base64 使用 python 2.7 编码,解码,从文件中提取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734401/
我有以下 json: {"results": [{"columns":["room_id","player_name","player_ip"], "types":["integer","text
我在 go 中获取格式不一致的 JSON 文件。例如,我可以有以下内容: {"email": "\"blah.blah@blah.com\""} {"email": "robert@gmail.com
JavaScript中有JSON编码/解码base64编码/解码函数吗? 最佳答案 是的,btoa() 和 atob() 在某些浏览器中可以工作: var enc = btoa("this is so
我在其中一个项目中使用了 Encog,但在解码 One-Of Class 时卡住了。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预
在我的 previous question关于使用 serialize() 创建对象的 CSV 我从 jmoy 那里得到了一个很好的答案,他推荐了我的序列化文本的 base64 编码。这正是我要找的。
有些事情让我感到困惑 - 为什么 this image在每个浏览器中显示不同? IE9(和 Windows 照片查看器)中的图像: Firefox(和 Photoshop)中的图像: Chrome(和
是否可以在不知道它的类型( JAXBContext.newInstance(clazz) )的情况下解码一个类,或者什么是测试即将到来的正确方法? 我确实收到了从纯文本中解码的消息 - 字符串 传入的
我正在尝试使用 openSSL 库进行 Base64 解码,然后使用 CMS 来验证签名。 下面的代码总是将缓冲区打印为 NULL。 char signed_data[] = "MIIO"; int
我有一个带有 SEL 类型实例变量的类,它是对选择器的引用。在encodeWithCoder/initWithCoder中,如何编码/解码这种类型的变量? 最佳答案 您可以使用 NSStringFro
var url = 'http://www.googleapis.com/customsearch/v1?q=foo&searchType=image'; window.fetch(url) .t
我想知道Android 2.2、2.3和3,4支持的音频/视频格式列表。我也想知道哪些Android版本支持视频编码和解码。我经历了this link,但是关于编码和解码我并不清楚。 任何人的回答都是
我在其中一个项目中使用 Encog,但在解码 One-Of 类时遇到了困难。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预测值。如
我正在尝试解码现有的 xml 文件,以便我可以正确处理数据,但 XML 结构看起来很奇怪。下面是 xml 示例以及我创建的对象。 11 266 AA1001 1
对 unicode 字符进行 URL 编码的常用方法是将其拆分为 2 %HH 代码。 (\u4161 => %41%61) 但是,unicode在解码时是如何区分的呢?您如何知道 %41%61 是 \
我正在尝试将 json 字符串解码为 Map。 我知道有很多这样的问题,但我需要非常具体的格式。例如,我有 json 字符串: { "map": { "a": "b",
我有一个查询,我认为需要像这样(解码会更大) SELECT firstName, lastName, decode(mathMrk, 80, 'A', mathMrk) as decodeMat
我知道PHP函数encode()和decode(),它们对我来说工作得很好,但我想在url中传递编码字符串,但encode确实返回特殊字符,如“=”、“”' “等等...... 这显然会破坏我的脚本,
我必须解码 Basic bW9uTG9naW46bW9uTW90RGVQYXNz 形式的 http 请求的授权 header 当我解码它时online ,我得到了正确的结果 monLogin:monM
这个问题已经有答案了: Decode Base64 data in Java (21 个回答) 已关闭 8 年前。 我想知道使用哪个库进行 Base64 编码/解码?我需要此功能足够稳定以供生产使用。
我正在尝试从 Arduino BT 解码 []byte,我的连接完美,问题是当我尝试解码数组时。我得到的只是这个字符�(发送的字节数相同)我认为问题出在解码上。我尝试使用 ASCII 字符集,但仍然存
我是一名优秀的程序员,十分优秀!