Last active
June 13, 2026 05:57
-
-
Save maddisondesigns/617274d4f82922824dd5d9afa4c87952 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Secure Encrypt Script using 7z | |
| # 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>]" | |
| 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 | |
| $ZIPLOCATION/7zz a -x\!".DS_Store" -r0 -t7z -mx=9 -m0=lzma2 -mhe=on -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