gpt4 book ai didi

python - base64 使用 python 2.7 编码,解码,从文件中提取文件

转载 作者:行者123 更新时间:2023-11-28 22:50:22 25 4
gpt4 key购买 nike

我已经阅读了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com