Python - mechanism to identify compressed file type and uncompress

前端 未结 7 2083
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 17:35

A compressed file can be classified into below logical groups
a. The operating system which you are working on (*ix, Win) etc.
b. Different types of compression algo

7条回答
  •  借酒劲吻你
    2020-12-12 18:13

    The accepted solution looks great, but it doesn't work with python-3, here are the modifications that made it work -- using binary I/O instead of strings:

    magic_dict = {
        b"\x1f\x8b\x08": "gz",
        b"\x42\x5a\x68": "bz2",
        b"\x50\x4b\x03\x04": "zip"
        }
    ''' SKIP '''
        with open(filename, "rb") as f:
    ''' The rest is the same '''
    

提交回复
热议问题