Created
April 18, 2023 15:13
-
-
Save evu/a15df504d11889b72614abbe3a1a24af to your computer and use it in GitHub Desktop.
In-mem compression / decompression and json serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import io | |
import json | |
import urllib.request | |
import zipfile | |
from pathlib import Path | |
from urllib.parse import urlparse | |
def get_file_from_uri(uri): | |
"""Get a file from a URI and return it as a string.""" | |
return urllib.request.urlopen(uri).read().decode("utf-8") | |
# Create a buffer | |
zip_buffer = io.BytesIO() | |
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, allowZip64=False, compresslevel=9) as zip_file: | |
for uri in urls: | |
# Extract filename from URI | |
filename = Path(urlparse(uri).path).name | |
# GET file as str | |
s = get_file_from_uri(uri) | |
# Convert to bytes | |
data = io.BytesIO(bytes(s, encoding="utf-8")) | |
# Write bytes to zipfile as filename | |
zip_file.writestr(filename, data=data.getvalue()) | |
# Validate zip archive | |
zip_file.testzip() | |
# Write the zip file directly | |
with open("pre_json.zip", "wb") as f: | |
f.write(zip_buffer.getvalue()) | |
######### Convert to json ######### | |
# Base64-encode the zipfile and convert it to string | |
zipped_str = base64.b64encode(zip_buffer.getvalue()).decode("ascii") | |
# Create json | |
jsonstr = json.dumps({"result": zipped_str}) | |
# Convert back from json, extract zipfile, and save zipfile to disk | |
with open("post_json.zip", "wb") as f: | |
f.write(base64.b64decode(json.loads(jsonstr)["result"])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment