zip - macOS


Overview

zip is a macOS command that compresses files and directories into ZIP archives. It is commonly used for archiving multiple files together, reducing their size for easier storage and transfer.

Syntax

zip [options] zipfile input_files

Options/Flags

  • -r, –recurse-directories: Recursively add files from subdirectories.
  • -q, –quiet: Suppress progress information.
  • -f, –force: Overwrite existing files without prompting.
  • -v, –verbose: Display detailed progress information.
  • -T, –test: Test the integrity of a ZIP archive without modifying it.
  • -l, –list: Display the contents of a ZIP archive.
  • -i, –include: Specify a list of patterns to include in the archive.
  • -x, –exclude: Specify a list of patterns to exclude from the archive.
  • -C, –directory: Set the root directory for the archive (default: current directory).
  • -m, –move: Move input files to the archive (delete originals).
  • -j, –junk-paths: Remove directory structure from file paths.
  • -z, –zip64: Use ZIP64 extensions to support archives larger than 4GB.

Examples

Create a ZIP archive from a single file:

zip example.zip file.txt

Recursively compress all files in a directory:

zip -r archive.zip directory

Compress selected files from a directory:

zip archive.zip -i "*.txt" directory

Exclude a specific file from the archive:

zip archive.zip -x "excluded_file.txt" directory

Test the integrity of a ZIP archive:

zip -T archive.zip

Common Issues

  • Permission denied: Ensure you have sufficient permissions to create or modify the ZIP archive.
  • Too many files or folders: ZIP archives have a maximum size limit. Use -z for archives larger than 4GB.
  • Invalid file path: Verbose mode (-v) can provide more details on file path issues.

Integration

Combine with tar:

tar -cvf - directory | zip archive.zip -

Use with find:

find directory -print0 | zip -0 archive.zip
  • gzip – Compress individual files.
  • tar – Create and extract tar archives.
  • unar – Extract archives of various formats.