Skip to main content

A file archiver with high compression ratio.

# To archive a file or directory:
7z a <archived.7z> <path/to/file_or_directory>

# To encrypt an existing archive (including headers):
7z a <encrypted.7z> -p<password> -mhe=on <archived.7z>

# To extract an existing 7z file with original directory structure:
7z x <archived.7z>

# To extract an archive with user-defined output path:
7z x <archived.7z> -o<path/to/output>

# To extract an archive to stdout:
7z x <archived.7z> -so

# To archive using a specific archive type:
7z a -t<zip|gzip|bzip2|tar> <archived.7z> <path/to/file_or_directory>

# To list available archive types:
7z i

# To list the contents of an archive file:
7z l <archived.7z>

# ---

#
# A couple bash 7z helper utils
#

# https://jonlabelle.com/snippets/view/shell/7z-command
function 7z_compress()
{
    if [ -z "$1" ]; then
        echo 'Usage: 7z_compress <path>'
        echo ''
        echo '       Compress the specified path with 7-Zip and create an archive'
        echo '       file in the working directory using the path BASENAME(1);'
        echo '       e.g.: <path_basename>.7z'
    else
        local archive_name
        archive_name="$(/usr/bin/basename "${1}").7z"
        /usr/local/bin/7z a "${archive_name}" "${1}"
    fi
}

# https://jonlabelle.com/snippets/view/shell/7z-command
function 7z_extract()
{
    if [ -z "$1" ]; then
        echo 'Usage: 7z_extract <compressed.7z>'
        echo ''
        echo '       Extract the specified 7-Zip compressed file in the working directory'
        echo '       maintaining the original directory structure.'
    else
        /usr/local/bin/7z x "${1}"
    fi
}