In [ ]:
import base64
import zipfile
from io import BytesIO
from IPython.display import HTML, display
qb = QuantBook()
folder_name = "Database"
file_name = "financials_and_prices.db"
object_store_db_key = f"{folder_name}/{file_name}"
zip_file_name = "financials_and_prices.zip"
# Use ContainsKey (standard QuantConnect API)
if qb.ObjectStore.ContainsKey(object_store_db_key):
# 1. THE FIX: Read as raw binary bytes instead of text
sqlite_bytes = qb.ObjectStore.ReadBytes(object_store_db_key)
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf:
# 2. writestr safely accepts byte arrays, preserving the SQLite architecture
zf.writestr(object_store_db_key, sqlite_bytes)
zip_bytes = zip_buffer.getvalue()
b64 = base64.b64encode(zip_bytes).decode('utf-8')
html_payload = f'''
<a download="{zip_file_name}"
href="data:application/zip;base64,{b64}"
target="_blank"
style="padding: 10px 15px; background-color: #1a73e8; color: white; text-decoration: none; border-radius: 4px; font-weight: bold;">
⬇️ Download {zip_file_name}
</a>
'''
display(HTML(html_payload))
print("Click the button to download your SQLite database.")
else:
print(f"File '{object_store_db_key}' not found in the Object Store.")