I'm working with a database column that has already been implemented as a varchar(MAX). I would like to compress the string data we normally save in this field to save space and increase load times when working with the database.
I used the zlib
library in python to do something like this:
compressed_string = zlib.compress(original_string.encode()) # The data type is now converted to bytes
database_string = str(compressed_string) # Converts the compression data into a string
I can save this data to the database within the pre-existing vachar(MAX) column when I use str()
, but I can't find a way to get back to my starting point after casting to a string.
If I do something like compressed_string.decode()
to try and get the string representation of the compression bytes, I get errors saying 'utf-8' codec can't decode byte 0x9c in position 1: invalid start byte
I also tried using ASCII encoding to no avail.
My end goal would be to get something like this to happen:
original_string = zlib.decompress(database_string).decode() # database_string needs to be a bytes-like object though
Does anyone know how I can store the bytes-like string representation of the compression and then reverse the process later?