DEV Community 👩‍💻👨‍💻

Chris Watson
Chris Watson

Posted on

Safari-like archive handling in Linux

Just wanted to share this, because I spent all morning figuring it out for myself. As someone that's used macOS a lot recently one of the features I really appreciate is Safari's ability to automatically extract archives when you open them. Granted that's about the only feature I appreciate about Safari, but I'll give credit where credit is due.

Having recently reinstalled Arch on my home workstation this is one the the features that I've really missed, so this morning I decided to add the feature myself.

Starting off I knew I'd most likely need to write a script and somehow associate the mime-types of the various archives I want to open with that script. Keep in mind that these instructions are known to work on GNOME and require the zsh shell. Different types of archives also require different programs to open them.

Here are the 2 files you're going to need. I'll explain them a bit further down.

First is extract.zsh (place this in ~/.local/bin):

And then the desktop file (place this in ~/.local/share/applications:

How does it work?

Honestly it's super simple, and makes use of some existing code from Oh-My-ZSH. extract.zsh checks the extension of the file you're trying to extract and matches it with the correct program for opening that file. This script was meant to be used as a zsh plugin, but I repurposed it. In doing so I needed to change the output directory (initially files were being extracted to my user's home directory) and make it automatically overwrite existing directories since we wanted this process to be automatic.

The .desktop file allows the script to be registered as the default program for a given mime-type. All you have to do is open one archive with a given extension using the extract menu option and that's it, extract is now the default program for opening those archives.

Now for the little bit of setup:

Any archive formats you want to have automatically extract need to be opened with extract so that the default for that archive type can be changed. To do this (on GNOME) right click an archive, select "Open With Other Application", click "Find New Applications" at the bottom of the dialog, and then find extract in the list. If you don't see it you may need to log out and then log back in again.

That's really it. Now download an archive and try opening it directly from the downloads menu in your browser. The archive should be extracted and then the directory automatically opened.

Top comments (0)

🌙 Dark Mode?!

Turn it on in Settings