如果字符串是zlib压缩的,如何快速检测?

Lou*_*fre 8 python zlib

python中确定字符串是否被zlib压缩的最快方法是什么.我目前正在使用它.

def iscompressed(data):
    result = True
    try:
        s =zlib.decompress(data)
    except:
        result = False  
    return result
Run Code Online (Sandbox Code Playgroud)

我相信有更优雅的方式.

Tob*_*ski 10

您可以检查前2个字节的标题信息 - 但是,它不是100%安全的.

http://www.faqs.org/rfcs/rfc1950.html,第2.2章