Zip multiple files into individual archives via Automator

Aug 03, '07 07:30:04AM

Contributed by: robg

A user over on the Macworld forums wanted to compress a number of individual files into separate zip archives. In OS X, if you select a number of files and use the Create Archive command, it compresses all those files into one large archive.

After a bit of work in Terminal and Automator, I came up with a simple one-step Automator Finder plug-in to enable this "zip into separate archives" functionality. Launch Automator, and drag the Automator » Run Shell Script action into the work area. Leave the shell set to /bin/bash, and set the Pass Input pop-up to as arguments. For the actual script, use this code:

for f in "$@"
do
    zip "$f.zip" "$f"
done
That's it; select File » Save as Plug-in, name your plug-in (Zip individually or whatever), and make sure the 'Plug-in for' pop-up is set to Finder. To use the workflow, select a number of files (this workflow will not work with folders!) and then control-click on the selection. In the contextual menu, choose Automator » Zip individually (or whatever you named it). A new archive named "filename.whatever.zip" will be created for each file in the selection.

Note that zip will include full directory structure information by default, so when you unzip the new archive, the files will each expand into a series of folders (created as necessary) that match where they were originally found. If you'd rather have the archives expand into their current directory, change the zip command above to this:
zip -j "$f.zip" "$f"
If you have a better way of doing this (that won't require the user to use Terminal), please post in the comments. In particular, if there's a way to structure this so it works with folders as well as files, I'd be interested in seeing how that works. If you're going to use this in a critical process, I suggest testing it extensively first. I ran a number of files through the compression and then expanded them again, and they all seemed to work. However, I didn't test every file type nor every possible usage scenario. (The original files aren't touched by the script, so there's no danger of losing them.)

Comments (19)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070803053156346