Skip to content

Instantly share code, notes, and snippets.

@maddisondesigns
Last active June 13, 2026 05:57
Show Gist options
  • Select an option

  • Save maddisondesigns/ad69de13203ba60a8cea89f8027e85f5 to your computer and use it in GitHub Desktop.

Select an option

Save maddisondesigns/ad69de13203ba60a8cea89f8027e85f5 to your computer and use it in GitHub Desktop.
macOS Command Line Zip script using 7z with max compression, AES-256, header encryption & PW Protection, also splitting zip into 500MB volumes
#!/bin/bash
# Secure Encrypt Script using 7z and splitting zip into 500MB volumes
# Requires: 7z (7-Zip) is not installed. Download it from 7-zip.org
# Exit on any error
set -euo pipefail
# Specify the folder location of the 7zip executable file
ZIPLOCATION=$HOME"/Downloads/7Zip"
# Check if 7z is installed
if ! command -v $ZIPLOCATION/7zz &> /dev/null; then
echo "❌ 7z (7-Zip) is not installed. Download it from 7-zip.org"
exit 1
fi
# Check for correct usage
if [ $# -lt 2 ]; then
echo "❌ No paramaters specified"
echo "Usage: $0 <output_name.7z> <source_file_or_folder> [<optional_source_file_or_folder>]"
echo "Note: Will split zip into 500M volumes"
exit 1
fi
COUNTER=0
OUTPUT="$1"
FILESNOTEXIST=0
# Prompt for password (secure input)
read -rsp "Enter password: " PASSWORD
echo
read -rsp "Confirm password: " PASSWORD_CONFIRM
echo
if [ "$PASSWORD" != "$PASSWORD_CONFIRM" ]; then
echo "❌ Passwords do not match!"
exit 1
fi
# Check if source exists
for SINGLEFILE in "$@"
do
COUNTER=$((COUNTER + 1))
if [ $COUNTER -gt 1 ]; then
# Check if source exists
if [ ! -e "$SINGLEFILE" ]; then
echo "Source '$SINGLEFILE' does not exist."
FILESNOTEXIST=1
fi
fi
done
# Exit if one or more source files do not exist
if [ 1 -eq "$FILESNOTEXIST" ]; then
echo "❌ One of more source files do not exist."
exit 1
fi
# Prevent overwriting existing archive
if [ -e "$OUTPUT" ]; then
echo "⚠️ Output file '$OUTPUT' already exists. Append to existing file? (Y/N): "
read -r CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "❌ Aborted to prevent overwrite."
exit 1
fi
fi
# Run the 7z command with max compression, AES-256, and header encryption & PW Protection & volume splitting
$ZIPLOCATION/7zz a -x\!".DS_Store" -r0 -t7z -mx=9 -m0=lzma2 -mhe=on -v500m -p"$PASSWORD" "$@"
# Clear password variables from memory
unset PASSWORD
unset PASSWORD_CONFIRM
echo "✅ Encrypted archive created: $OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment