How to quickly detect if a zlib string is compressed?

What is the fastest way in python to determine if a zlib string has been compressed. I am using this currently.

def iscompressed(data): result = True try: s =zlib.decompress(data) except: result = False return result 

I am sure there is a more elegant way.

+7
source share
2 answers

You can check the first 2 bytes for header information - it is, however, not 100% secure.

See http://www.faqs.org/rfcs/rfc1950.html , chapter 2.2

+10
source

While the only way to be 100% sure to actually try to unpack it, you can make a reasonable guess by looking at the zlib + flags compression bit:

http://www.faqs.org/rfcs/rfc1950.html

+2
source

All Articles