Compress files from OS X terminal?

In the Finder, there is this wonderful ability to right click on a file or directory, select compress from the drop-down, and end up with a zipped file.

Is it possible to do the same thing from the terminal?


Solution 1:

It's called zip.

This adds the file file to the archive file.zip:

zip file.zip file

Of course, to add more files, just add them as arguments to the command. Check out man zip for more options.

Often, you'll want to skip including those pesky .DS_Store files, for example compressing the whole folder folder into folder.zip:

zip -vr folder.zip folder/ -x "*.DS_Store"

Solution 2:

To compress the files exactly as the Finder command would compress them, use:

ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip

See man ditto for details:

 The command:
       ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
 will create a PKZip archive similarly to the Finder's Compress function-
 ality.