Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect quickly if a string is zlib compressed?

Tags:

python

zlib

What's the quickest way in python to determine if a string was compressed by zlib. 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.

like image 651
LouisChiffre Avatar asked Mar 16 '11 08:03

LouisChiffre


People also ask

How do you check if a string is compressed?

Use mb_check_encoding to see whether a string is valid in the encoding you suspect it to be in. If it isn't, it's probably compressed (or you checked for the wrong encoding). With the caveat that virtually any byte sequence is valid in virtually every single-byte encoding, so this'll only work for multi-byte encodings.

Is zlib fast?

Arm throughput (MB/s, higher is better): At level 6, zlib-cloudflare is, on average, 90 percent faster in compression operations than zlib-madler. zlib-cloudflare is, on average, 52 percent faster in decompression operations than zlib-madler.

What compression does zlib use?

As of September 2018, zlib only supports one algorithm, called DEFLATE, which uses a combination of a variation of LZ77 (Lempel–Ziv 1977) and Huffman coding. This algorithm provides good compression on a wide variety of data with minimal use of system resources.

Is zlib compression lossless?

zlib is a free, open-source software for the compression and decompression of files. Additionally, it comes under the lossless data compression category.


2 Answers

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

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

like image 62
Tobias Schittkowski Avatar answered Oct 18 '22 22:10

Tobias Schittkowski


While the only way to be 100% sure is to actually try to decompress it, you can make a reasonable guess by looking for the zlib compression method + flags header bits:

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

like image 24
Amber Avatar answered Oct 18 '22 22:10

Amber