Skip to content

Instantly share code, notes, and snippets.

@penut85420
Created May 16, 2025 02:35
Show Gist options
  • Select an option

  • Save penut85420/ffc88913f3d728f27800d2a63525df1b to your computer and use it in GitHub Desktop.

Select an option

Save penut85420/ffc88913f3d728f27800d2a63525df1b to your computer and use it in GitHub Desktop.
A simple encoder/decoder using Base64 and Gzip.
import base64
import gzip
import fire
def encode_text(src_path, dst_path="encode.txt"):
with open(src_path, "rb") as fp:
data = fp.read()
data = gzip.compress(data)
data = base64.b64encode(data)
data = data.decode("UTF-8")
with open(dst_path, "wt", encoding="UTF-8") as fp:
fp.write(data)
def decode_text(src_path="encode.txt", dst_path="decode.txt"):
with open(src_path, "rt", encoding="UTF-8") as fp:
data = fp.read()
data = data.encode("UTF-8")
data = base64.b64decode(data)
data = gzip.decompress(data)
with open(dst_path, "wb") as fp:
fp.write(data)
if __name__ == "__main__":
fire.Fire(dict(encode=encode_text, decode=decode_text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment