Search code examples
rfilecompressionbzip2

How do I check a file's magic number (or hex signature) in R?


I'd like to detect the file type of an object in order to determine the right connection for a compressed file (e.g. whether to use bzfile gzfile, xzfile etc. Obviously I could "guess" based on the file extension, but my understanding is that the checking the magic number would be more robust (though I'm a little unclear if that's portable to Windows-based architectures?)

How do I return the magic number of a file to determine if it is, say, is indeed BZh?

Note that I'd like a solution that is suitable for use in package development; e.g. will be as portable as possible across operating systems and not specific to *nix systems.


Solution

  • There are several packages on Github that make use of libmagic some way or other: