So I am trying to compress (gzip or similar format) a JSON object before I throw it in my MySQL database. I am currently storing the data as BLOB. I have tried to use the following Java method to compress the data:
public static byte[] compress(String str) throws Exception {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream obj = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(obj);
gzip.write(str.getBytes("UTF-8"));
gzip.close();
return obj.toByteArray();
}
and then store it in the database using setBytes()
with a PreparedStatement
and am having no issues with this. What I am having issues with is decrypting the data in Python 2.7 I have tried using zlib.decompress()
to no avail. It can't seem to read the data that Java is storing. I also need to write a conversion script in Python to compress the old rows into this new format. So whatever format I needs to be readable by the Python decompress()
whether it was compressed with Java or Python 2.7
I am happy to provide anymore information that can assist in helping to find a solution to my dilemma.
Thanks.
EDIT: Some of the Python Code:
class KitPvPMatch(Base):
""" The SQLAlchemy declarative model class for a User object. """
__tablename__ = 'kit_pvp_matches'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
match_id = Column(INTEGER(11), autoincrement=True, primary_key=True, nullable=False)
season = Column(Unicode(5), nullable=False)
winner = Column(Unicode(16), nullable=False)
loser = Column(Unicode(16), nullable=False)
ladder_id = Column(TINYINT(4), nullable=False)
data = Column(BLOB, nullable=False)
# The line in question
jsonData = json.loads(zlib.decompress(match.data))
# The error
error: Error -3 while decompressing data: incorrect header check