Python Forum
detecting type of compression of a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
detecting type of compression of a file
#1
i would like a library function that can read the first 4096 bytes of a named file (no more than that, less if it can) and detect if it is a compressed file or not and return an indicator of which type of compression or not..

0 for not compressed
1 ... for compressed

or names may be given as strings. i do not want to have to call some program since i would like for this to be portable. it should support gzip, bzip2, lzma, pkzip, and xz. and anything else Python can support. an alternative is one that can determine this from a bytes string of the starting contents of the file (the caller reads in some of the file).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is an experiment in a linux console
$ cp spam.zip foo
$ truncate -s 4096 foo
$ file foo
foo: Zip archive data, at least v2.0 to extract
Calling Linux' file command could give you the functionality.

Python libraries may exist in Pypi such as python-magic or filetype
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  default mode in compression modules Skaperen 0 1,283 Nov-20-2020, 03:12 AM
Last Post: Skaperen
  detecting input that is not compressed Skaperen 0 1,377 Nov-20-2020, 12:19 AM
Last Post: Skaperen
  getting file type information Skaperen 5 3,127 Aug-22-2019, 10:53 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020