Open (un)compressed file in Emacs

April 26, 2020

In Emacs, when you open compressed file like foo.txt.gz, it will transparently decompress it (using gzip in case above) and open as normal file. However, what will happen if above file is not compressed at all?

Here is my case. I'm using excellent rclone to open remote S3 drives by mounting them in local folder. From there, Emacs and dired works much better than any TRAMP hack I tried.

However, application that would ship logs to S3, would ship them with gzip extension attached (hence foo.txt.gz), but files would not be compressed. I'm not sure if this behavior is intentional or bug in the script, but this will cause Emacs to fail with this error:

Error while executing "gzip -c -q -d < foo.txt.gz"

gzip: stdin: not in gzip format

Unsurprisingly, vim will fail as well, but less will open it correctly because it looks in file header, not extension.

Emacs has find-file-literally for cases like this - it will open file as is without applying any kind of conversion. Usually first choice and excellent tool for debugging malformed content.

But, other modes like dired or VLF will not work, because they are using find-file for opening files, and this function will try to apply all conversion magic usually we get when Emacs opens a file.

What worked in my case is this: I disabled auto-compression-mode, which is enabled by default, calling M-x auto-compression-mode. And suddenly dired and VLF, start openning those files without problems.

Just to remind you to not forget to enable auto-compression-mode again or reading links from Emacs documentation (e.g. M-x describe-function), maybe even other stuff, will silently stop working ;)