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/5f5c1724ef913776c39552f2bc7d455f to your computer and use it in GitHub Desktop.

Select an option

Save maddisondesigns/5f5c1724ef913776c39552f2bc7d455f to your computer and use it in GitHub Desktop.
macOS Command Line Zip Secure Extract Script using 7z
#!/bin/bash
# Secure Extract Script using 7z
# Requires: 7z (7-Zip) is not installed. Download it from 7-zip.org
# Exit on error or undefined variable
set -euo pipefail
# Specify the folder location of the 7zip executable file
ZIPLOCATION=$HOME"/Downloads/7Zip"
# Usage check
if ! command -v $ZIPLOCATION/7zz &> /dev/null; then
echo "❌ 7z (7-Zip) is not installed. Download it from 7-zip.org"
exit 1
fi
# Input check
if [ $# -lt 1 ]; then
echo "❌ No paramaters specified"
echo "Usage: $0 /path/to/archive.7z"
exit 1
fi
ARCHIVE="$1"
# Check file exists and is a regular file
if [ ! -f "$ARCHIVE" ]; then
echo "❌ File not found or not a regular file: $ARCHIVE"
exit 1
fi
# Prompt for password (hidden input)
read -rsp "🔓 Enter password to decrypt: " PASSWORD
echo
# Extract to a directory with same name as archive (without .7z)
OUTPUT_DIR="${ARCHIVE%.7z}_extracted"
# Create output directory if it doesn’t exist
mkdir -p "$OUTPUT_DIR"
# Run extraction securely
if $ZIPLOCATION/7zz x -p"$PASSWORD" "$ARCHIVE" -o"$OUTPUT_DIR"; then
echo "✅ Extraction complete. Files saved to: $OUTPUT_DIR"
else
echo "❌ Extraction failed. Possibly wrong password or corrupted archive."
exit 1
fi
# Unset password variable
unset PASSWORD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment