14

I noticed that the command line 'zip' tool and Mac OS X's 'Compress XXX' option (available via right click in finder) are giving different output files. Not only is the size of the file a few hundred bytes bigger but the content is significantly different as well.

How can I find out what command the Finder is using for compression?

Locksleyu
  • 5,192
  • 8
  • 52
  • 77
  • possible duplicate of [How to create a zip file in the same format as the Finder's "Compress" menu item?](http://stackoverflow.com/questions/107903/how-to-create-a-zip-file-in-the-same-format-as-the-finders-compress-menu-item) – ceejayoz May 24 '12 at 13:33

2 Answers2

25

The answer is in man ditto:

 The command:
       ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
 will create a PKZip archive similarly to the Finder's Compress function-
 ality.
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Does this just mean the default compression algorithm the `zip` command uses is _better_ than Finder's Compress functionality? Because I consistently find that the archive is a bit smaller when created via the `zip` command. – fullStackChris Oct 12 '22 at 06:27
  • 1
    Not really, no. `ditto` uses the same algorithm. What you're seeing may just be different settings for the compression level. You can use `zip -9` or `ditto --zlibCompressionLevel 9` for the best (and slowest) compression in both cases. Also `ditto` may be saving more stuff than zip, e.g. resource forks, extended attributes, and access information. – Mark Adler Oct 12 '22 at 06:55
4

Take a look at An AppleScript to compress a Finder selection article.

try
    tell application "Finder"
        set theSelection to the selection
        set selectionCount to count of theSelection
        if selectionCount is greater than 1 then
            error "Please select only one Finder item before running this script."
        else if selectionCount is less than 1 then
            error "Please select one Finder item before running this script."
        else
            set theItem to (item 1 of theSelection) as alias
            set destFolder to (container of theItem) as alias
            set itemName to name of theItem
        end if
    end tell

    do shell script ("ditto -c -k --sequesterRsrc --keepParent " & quoted form of POSIX path of theItem & space & quoted form of (POSIX path of destFolder & itemName & ".zip"))
on error theError
    tell me
        activate
        display dialog "Error: " & theError buttons {"OK"} default button 1 with icon stop
    end tell
end try
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144