How to gzip a bytearray in Python?

孤者浪人 提交于 2019-12-10 21:19:18

问题


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?


回答1:


Have a look at the zlib-module of Python.

Python 3: zlib-module

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)

Python 2: zlib-module

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.




回答2:


    import zlib 
    import binascii


def compress_packet(packet):
    return zlib.compress(buffer(packet),1)

def decompress_packet(compressed_packet):
    return zlib.decompress(compressed_packet)

    def demo_zlib() :

        packet1 = bytearray()
        packet1.append(0x41)
        packet1.append(0x42)
        packet1.append(0x43)
        packet1.append(0x44)

        print "before compression: packet:{0}".format(binascii.hexlify(packet1))
        cpacket1 = compress_packet(packet1)
        print "after compression: packet:{0}".format(binascii.hexlify(cpacket1))

        print "before decompression: packet:{0}".format(binascii.hexlify(cpacket1))
        dpacket1 = decompress_packet(buffer(cpacket1))
        print "after decompression: packet:{0}".format(binascii.hexlify(dpacket1))




    def main() :
        demo_zlib() 

    if __name__ == '__main__' :
        main() 

This should do. The zlib requires access to bytearray content, use buffer() for that.




回答3:


In case the bytearray is not too large to be stored in memory more than once and known as b, you can just:

b_gz = str(b).encode('zlib')

If you need to do deocding first, have a look at the decode() method of the bytearray.




回答4:


The zlib module of Python Standard Library should meet your requirements :

>>> import zlib
>>> a = b'abcdefghijklmn' * 10
>>> ca = zlib.compress(a)
>>> len(a)
140
>>> len(ca)
25
>>> b = zlib.decompress(ca)
>>> b == a
True
>>> b
b'abcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn'

This is the output under Python3.4, but it works same under Python 2.7 -



来源:https://stackoverflow.com/questions/26753147/how-to-gzip-a-bytearray-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!