Base64 Streaming I/O

1.0.3 · active · verified Thu Apr 16

`base64io` is a Python library that provides a file-like object interface for streaming Base64 encoding and decoding. It allows processing large files without loading their entire content into memory by wrapping an existing byte stream. The current version is 1.0.3, and due to its stable and specialized utility, releases are infrequent.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `base64io.Base64IO` to stream encode and decode content. It creates dummy binary and Base64 files, then uses `Base64IO` to transform them, verifying the output. Note that `Base64IO` always expects and produces byte streams.

import base64io
import tempfile
import os

# Create dummy input files in a temporary directory
dummy_input_bin_content = b"Hello, base64io streaming!"
dummy_input_b64_content = b"SGVsbG8sIGJhc2U2NGlvIHN0cmVhbWluZyE=" # Base64 of the above

with tempfile.TemporaryDirectory() as tmpdir:
    input_bin_path = os.path.join(tmpdir, 'input.bin')
    input_b64_path = os.path.join(tmpdir, 'input.b64')
    output_b64_path = os.path.join(tmpdir, 'output.b64')
    output_bin_path = os.path.join(tmpdir, 'output.bin')

    # Write dummy content to temporary files
    with open(input_bin_path, 'wb') as f:
        f.write(dummy_input_bin_content)
    with open(input_b64_path, 'wb') as f:
        f.write(dummy_input_b64_content)

    print(f"Temporary directory: {tmpdir}")

    # --- Encoding example ---
    # Open original binary input, wrap with Base64IO for encoding
    with open(input_bin_path, 'rb') as fin, base64io.Base64IO(fin, 'encode') as fenc:
        # Write encoded content to output file
        with open(output_b64_path, 'wb') as fout:
            fout.write(fenc.read())
    
    # Verify encoded output
    with open(output_b64_path, 'rb') as f:
        encoded_output = f.read()
    print(f"Encoded content written to {output_b64_path}: {encoded_output}")
    assert encoded_output == dummy_input_b64_content

    # --- Decoding example ---
    # Open Base64 encoded input, wrap with Base64IO for decoding
    with open(input_b64_path, 'rb') as fin, base64io.Base64IO(fin, 'decode') as fdec:
        # Write decoded content to output file
        with open(output_bin_path, 'wb') as fout:
            fout.write(fdec.read())
            
    # Verify decoded output
    with open(output_bin_path, 'rb') as f:
        decoded_output = f.read()
    print(f"Decoded content written to {output_bin_path}: {decoded_output}")
    assert decoded_output == dummy_input_bin_content

print("Quickstart successful!")

view raw JSON →