The Robservatory

Robservations on everything…

 

Quickly expand compressed files in Finder

In this post, I lamented on the incredible slowdown seen when expanding many compressed files in Finder. To save a trip back to that article, here’s the tldr; version:

Expand 24 .gz files Finder:
12.8 seconds
Terminal:
.013 seconds
Terminal is 984.6x faster than Finder

With a bit of work, though, you can harness the power of Unix in Finder, and get both the GUI and the speed when expanding files. There are many ways to do this, including third-party apps such as The Unarchiver. You could also write an Automator Service to do the work for you. But I chose to write a Keyboard Maestro macro, because I could make the macro a menu bar option that only shows when Finder is frontmost:

As seen, I actually have two expanders, one that expands just the Apple sales reports mentioned in my original post, and the other that will handle any mix of compressed files. (Yes, the ‘all files’ macro could also handle the Apple sales reports, but because of how it has to run for multiple file types, it’s marginally slower than the dedicated macro.)

These macros both work in the same way—they call on Unix apps to do the expansion, bypassing Apple’s slow GUI expander (which is called Archive Utility). If you’re curious how they work, and/or would like to download them for your own use, keep reading.

Note: Both macros referenced below can be downloaded and installed for your testing, use, and modification.

Keyboard Maestro makes running shell scripts quite easy, and includes a built-in variable that provides the path to a file. As such, the macro to expand my App Store .gz archives is incredibly simple:

This macro uses the “For each item in a collection” action; one predefined collection is a selection in Finder. Assign any variable name, and then use the Execute Shell Script action (set to ‘execute text script’ mode) to pass that variable to the Unix command:

gzip -d -k "$KMVAR_FullFilePath"

(The quotes are needed to protect any spaces in filenames.)

And that’s all there is to it—using this macro, all I do is select the Apple-provided files in Finder, then choose the macro from Keyboard Maestro’s menu bar icon. There is a bit of overhead, so it takes somewhat longer than .013 seconds…but it’s still under a second (vs. nearly 13 for the Finder alternative). No GUI, no dancing dialog, just about a second’s worth of work, and I have my new files, ready to go.

Because this worked so well for me, I decided I wanted to write a macro that would handle any type of compressed file. This was somewhat trickier, because there’s not one Unix program that will expand all types of compressed files. I wound up creating a shell script that checks each file’s extension, and launches the proper program based on the extension. I then call that script from Keyboard Maestro.

The Keyboard Maestro portion of the macro looks quite similar to the dedicated .gz macro:

The real work happens in the unz script, which looks like this:

I borrowed most of that code from this post, then modified it to work with Keyboard Macro.

The one thing that tripped me up was that, unlike gzip, many of the expansion tools don’t seem to know the path to the files they’re uncompressing. As a result, my script was dying because it runs in the top-level folder when run via Keyboard Maestro. The solution was a little-known (to me, anyway) shell command, dirname. As you might guess from its name, dirname extracts the path to any file, stripping out the final slash and the filename. Using that command, I was able to extract the path into a variable…

workdir=dirname $KMVAR_FullFilePath

…then pass that variable to the expansion utilities as the destination directory:

...tar xjf $1 -C $workdir

Be aware that if you’re going to use this macro, it has a couple of disadvantages over Apple’s Archive Utility:

  • Some archives may expand into your Downloads folder, instead of their source folder.
  • Folder structures may not be retained for certain compression formats.

Maybe these problems are solvable, maybe they’re not…but I don’t need this macro very often, so it’s not worth much time to investigate fixing the problems—especially when this version runs so quickly!

As noted, you can download both macros (including the script file) if you want to use or modify them. Keep in mind that the path data for the script is hardcoded in the “all files” macro, so you’ll need to change that to make it work on your system.

Leave a Reply

The Robservatory © 2017 Built from the Frontier theme