Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Zip multiple files into individual archives via Automator Apps
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.)
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)
 
[35,746 views]  

Zip multiple files into individual archives via Automator | 19 comments | Create New Account
Click here to return to the 'Zip multiple files into individual archives via Automator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Zip multiple files into individual archives via Automator
Authored by: mrsparkl33 on Aug 03, '07 09:34:00AM

for folders, try the -r option ..
zip -r "$f" $f"

from the man page -- a little confusing that the args are "foo" and "foo". the first arg is turned into "foo.zip" .. the second arg is the file spec (of files which will be placed into the .zip)
~~~
To zip up an entire directory, the command:

zip -r foo foo

creates the archive foo.zip, containing all the files and direc-
tories in the directory foo that is contained within the current
directory.
~~~




[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: morespace54 on Aug 03, '07 11:30:01AM

Okay I must be doing something wrong because I did try to mix this hint (zip individual files) with the folder argument and it's not working...

I try both:

--
for f in "$@"
do
zip -r "$f" $f"
done
--

and

--
for f in "$@"
do
zip -r "$f.zip" "$f"
done
--

whitout any luck... :( I must admit I'm not used to play with Automator and Run Shell Script...



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: morespace54 on Aug 03, '07 02:14:22PM

Never mind.

I just tried the script for individual files with folders selected and it work just the same (~/foo/bar with foo selected become ~/foo.zip). Doh!



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: morespace54 on Aug 06, '07 08:17:37AM

Okay, one last thing... ;)

When I zip a folder with this script, let's say /foo from the Desktop level (~/Desktop/foo),
everything is working fine, i.e. it gives me a file named foo.zip at the same level as my original /foo folder (~/Desktop/foo).
The problem is when I unzip it. I end up with nested folders, i.e. /Users/MyUserName/Desktop/foo on the ~/Desktop folder. Is there a way to not include all those (almost empty) folders in the script?

The idea would be to make the script behave just like the "Create Archive of..." in the contextual menu



[ Reply to This | # ]
A variant
Authored by: Arlo on Aug 03, '07 09:50:32AM
The Automator workflow One Zipper does the same thing, but uses the cleaner 'xargs'. It's saved as a droplet, but can easily be converted to use as a contextual menu item.

---
www.automatorworld.com
"Better Living Through Macintosh Scripting"

[ Reply to This | # ]

Zip multiple files into individual archives via Automator
Authored by: Chas on Aug 04, '07 02:38:32PM

IMHO it is better to just throw them in a folder and zip the folder. It's my pet peeve when a bunch of files unzip and end up all over the place as individual files. I'd rather unzip them into a nice folder.



[ Reply to This | # ]
using filetests to zip both files and directories transparently
Authored by: klktrk on Aug 05, '07 02:22:17AM
In order to make this automator work flow work transparently with both files and directories, use file tests, like so:

for f in "$@"; do
	# make sure the file/directory still exists so no error is thrown
	if [ -e "$f" ]; then 
		# okay, is it a directory?
		if [ -d "$f"];then
			# zip as directory
			zip -r "$f" "$f"
		else
			# zip as file
			zip "$f" "$f"
		fi
	fi
done
Have not tested, but should work just fine.

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart

[ Reply to This | # ]

using filetests to zip both files and directories transparently
Authored by: LeeH on Aug 05, '07 12:57:28PM

Sorry, does not seem to work. Added .zip to two places and it would partially work.

However, what would be nice is to have the workflow go like this:

Set up: single file on desktop, folder on desktop with some files in it and another folder in it with some more file (i.e. a level 2)

Now, results: run the work flow and have it zip the single file. Next, it would zip the folder and the subfolder. This does not happen now - no second level.

Also, would like to stip the path out of the zip file just to the folder level. For the single file a -j seems to work. However, how do you get the file path to just start at the folder being zipped, not at the user/desktop/etc level when unzipping?

Can this be done?



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: lar3ry on Aug 06, '07 09:57:16AM
I use a workflow with the following script, which does the following:

  1. Handles both files and directories
  2. Uses bzip2 instead of zip (better compression)
  3. Files are replaced by original_filename.bz2
  4. Directories are replaced by original_directoryname.tar.bz2
  5. In the event of an error, the original is not deleted
  6. File names are quoted properly to take into consideration embedded blanks which would mess up other solutions I've seen
for i in "$@"
do
    if [ -d "$i" ]
    then
        d=`dirname "$i"`
        b=`basename "$i"`
        (cd "$d"; /usr/bin/tar -cvjf "$b".tar.bz2 "$b" && rm -rf "$b")
    else
        /usr/bin/bzip2 -v9 "$i"
    fi
done


[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: sstoy on Sep 25, '07 10:38:33AM

I would love it if this action worked, it would be perfect as i agree, i would like the extra compression afforded by it, however it simply does not work. . . do you think you could go over exactly what i should do in the automator terminal? as in should it be "/bin/bash" and "asas arguments" ? also i copied your code exactly and pasted it in the script box, perhaps i was supposed to do something else as well? thanks.



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: nocomprendo on Aug 22, '12 07:16:22AM

So many years have gone by and I still haven't found any better script to what our script does!
There is only one problem, when applied through Automator (on 10.6.8 or 10.5) is crashes after it compresses the 1st folder. Do you any idea why or if there is anything one can do to avoid this problem?



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: qrayg on Sep 04, '07 02:26:26PM

Chas, you are missing the point.

Let's say you have a game emulator and you have 1500 roms. The emulator accepts .zip and if you zip all of the roms you save a significant amount of space.

Zipping all of the files into a single zip will not work as the emulator will probably only load the first file in the zip.

This hint is very useful for us emu geeks.



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: sstoy on Sep 25, '07 11:08:39AM

This seems to work, but it only works if you select an entire folder and run the automator scrpt from there, otherwise if you select individual files it actually searches each file for folders and zips the files within the "package contents" : so workflow looks like this:

in the automator place action "get selected finder items"

followed by "get folder contents"

followed by "run shell script" : keep the shell as /bin/bash and the pass input as "to stdin" the actual script is

xargs -I {} zip {}.zip {}


that's it. save the workflow as a finder selection and select a folder full of files you wish to individually compress, and it works but yes unfortunately it still makes it have that annoying files structure, which i personally can't stand, and also it doesn't seem to like working with other folders within folders and it can't seem to compress .apps very well, lastly i would like to have the option to individually select groups of files from within a folder and zip those.i dunno what's up with that. so maybe someone can take this script and flesh it out? :)



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: cosmodo on Nov 05, '07 04:51:58AM

thanks, useful tip.

does anyone know a way to get around the directory structure... at the moment i have to dump my folders in the root dir to avoid the problems unzipping.



[ Reply to This | # ]
Zip multiple files into individual archives via Automator- and use encryption
Authored by: anonymous.messia on Feb 01, '09 05:49:24PM

and what if you want to encrypt each file with the same password?
just adding the -e-argument fails for obvious reasons.. so how?



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: Lou Kash on Mar 13, '09 05:37:55PM
A solution that works on Leopard by running an AppleScript instead of a Shell script can be found here:
http://discussions.apple.com/message.jspa?messageID=7079445


[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: applemacdave on May 04, '09 10:33:15AM
Can someone help me with the reverse of this? I have a folder with several zip files, would like to select all, run script to unzip each in the same folder as the zip files Everything I have tried, resorts to unzipping to the root of the user's home directory. Here is where I sit right now...

for f in "$@"
do
	unzip -d "$f"
done


[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: muppetdog on Sep 14, '10 09:26:14AM

found this tip via Google. you have no idea how much time you just saved me.

i actually prefer using Terminal/Shell for zipping files...some people with older Windows systems seem to struggle with zip files made on a Mac and using the shell helps.



[ Reply to This | # ]
Zip multiple files into individual archives via Automator
Authored by: megabulk on Oct 15, '11 01:03:44AM

for f in "$@"
do
    zip -r -j "$f" "$f"
done
will zip individual folders without the nested (absolute path) directory structure. Also, in Automator, make sure the "Pass Input" pulldown is set to "as arguments".

[ Reply to This | # ]