Mac – How to stop the hidden __MACOSX folder from being created when compressing files/folders in Finder

foldersmaczip

When compressing files/folders in Finder, via the context menu, i.e. select the files/folders then right-click and select Compress …, a hidden folder named __MACOSX is created within the zip archive.

How can the hidden folder named __MACOSX not be created within the zip archive?

Best Answer

Using info from the other answer, here's an Automator Service1 that becomes available in Finder to delete the "__MACOSX" meta-data folder from a zip archive file.

To Create the Service:

  1. Open Automator and select: File > New > Service

  2. Set Service receives selected to files or folders and in to Finder.

  3. Add a Run Shell Script Action, setting Shell: to /bin/bash and Pass input: to as arguments while replacing the default code with the following code:

    for f in "$@"; do
        if [[ ${f##*.} =~ ^[zZ][iI][pP]$ ]]; then
            zip -d "$f" "__MACOSX*"
        fi
    done
    afplay /System/Library/Sounds/Purr.aiff
    
    • What the code does: It checks that each file passed to the service has a zip extension, regardless of case, and if it has a zip extension, and if "__MACOSX" exists within the zip archive, deletes the "__MACOSX" meta-data folder from the zip archive.

    • ${f##*.} gets the file extension, =~ tests the regex, and the ^[zZ][iI][pP]$ regex matches any case combination of each letter within the square braces while ^ asserts position at start of the string and $ asserts position at the end of the string. So it only matches zip in any case combination of those letters.

    • Note: The last line of the code is optional, just to let you know the service completed, and can be changed to a different sound or omitted if you prefer not to be notified by sound upon completion.

  4. Save the Service as, e.g.,: Delete '__MACOSX' from Zip Archive

1 In macOS Mojave, and later, an Automator Service is called a Quick Action. There are also other minor nomenclature differences but they should be more obvious when comparing them to pre-macOS Mojave Automator workflows.

To Use the Service:

In Finder, or on the Desktop if the zip archive is located there, select the zip archive(s), then right-click and select Delete '__MACOSX' from Zip Archive from the context menu or under Services on the context menu, or from Services on the Finder menu as appropriate.

The image of the service in Automator, below, is from OS X 10.8.6, however, it was tested under macOS 10.12.5 and works there as well.

Automator Service