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.
There are several packages on Github that make use of libmagic some way or other:
libmagic
on *nix and uses file.exe
from Rtools on Windows.magic.mgc
.libmagic
on *nix and compiles libmagic
on Windows (c.f. https://github.com/daqana/dqmagic/tree/windows)