python gzip解压字节码,如何在Python中gzip一个字节数组?

I have binary data inside a bytearray that I would like to gzip first and then post via requests. I found out how to gzip a file but couldn't find it out for a bytearray. So, how can I gzip a bytearray via Python?

解决方案

Have a look at the zlib-module of Python.

A short example:

import zlib

compressed_data = zlib.compress(my_bytearray)

You can decompress the data again by:

decompressed_byte_data = zlib.decompress(compressed_data)

A short example:

import zlib

compressed_data = zlib.compress(my_string)

You can decompress the data again by:

decompressed_string = zlib.decompress(compressed_data)

As you can see, Python 3 uses bytearrays while Python 2 uses strings.

你可能感兴趣的:(python,gzip解压字节码)