Skip to main content

Compress/uncompress files with brotli compression.

# To compress a file, creating a compressed version next to the file:
brotli <file.ext>

# To decompress a file, creating an uncompressed version next to the file:
brotli -d <file.ext>.br

# To compress a file specifying the output filename:
brotli <file.ext> -o <compressed_file.ext.br>

# To decompress a brotli file specifying the output filename:
brotli -d <compressed_file.ext.br> -o <file.ext>

# To specify the compression level. 1=Fastest (Worst), 11=Slowest (Best):
brotli -q <11> <file.ext> -o <compressed_file.ext.br>

# ---

# https://jonlabelle.com/snippets/view/shell/brotli-command
function brotli_compress()
{
    if [ -z "$1" ]; then
        echo 'Usage: brotli_compress <file.ext>'
        echo ''
        echo '       Compress the specified file with Brotli placing it'
        echo '       in the current working directory;'
        echo '       ex: <file.ext.br>'
    else
        if [ ! -f "${1}" ]; then
            echo "Brotli can only compress a file or a single stream of data, not unlike GZIP(1)."
            return 1
        fi
        /usr/local/bin/brotli --quality 5 "${1}"
    fi
}

# https://jonlabelle.com/snippets/view/shell/brotli-command
function brotli_extract()
{
    if [ -z "$1" ]; then
        echo 'Usage: brotli_extract <compressed_file.ext.br>'
        echo ''
        echo '       Extract the specified Brotli compressed file in the'
        echo '       current working directory;'
        echo '       ex: <file.ext>'
    else
        /usr/local/bin/brotli --decompress "${1}"
    fi
}