Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python 2.7-compatible file-based/streaming base64 encode/decode (from https://stackoverflow.com/questions/22734401/base64-encode-decode-to-from-files-in-chunks-with-python-2-7)
# from https://stackoverflow.com/questions/22734401/base64-encode-decode-to-from-files-in-chunks-with-python-2-7
#
# write_base64_file_from_file
# write_file_from_base64_file
# - 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 base64
import shutil
def write_base64_file_from_file(src_fname, b64_fname, chunk_size=8192):
chunk_size -= chunk_size % 3 # align to multiples of 3
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)
def write_file_from_base64_file(b64_fname, dst_fname, chunk_size=8192):
chunk_size -= chunk_size % 4 # align to multiples of 4
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment