Spyke

Syndicated from the fediverse. Read and engage on the original instance.

View original on lemmy.world
shellscripting·Shell ScriptingbyWillieShen

Added zstd, 7z, and 7z non-solid support to compression and extraction bash functions

__pv() {
  if command -v pv >/dev/null 2>&1; then
    pv
  else
    cat
  fi
}

compress_single() {
  local msg='Usage: compress_single [-h|--help]
compress_single [-t|--tar] [-n|--no-tar] [-p|--pad PAD] COMMAND SOURCE [TARGET]
If TARGET is not provided and PAD is provided, SOURCE concatenating PAD is used.
No tar is used by default.'
  local tar=0
  local pad=''
  local target=''
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -h | --help)
        echo "$msg"
        return 0
        ;;
      -t | --tar)
        tar=1
        shift
        ;;
      -n | --no-tar)
        tar=0
        shift
        ;;
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          echo "$msg" >&2
          return 1
        fi
        pad="$2"
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  if [ "$#" -eq 3 ]; then
    target="$3"
  elif [ "$#" -eq 2 ] && [ -n "$pad" ]; then
    target="${2}${pad}"
  else
    echo "$msg" >&2
    return 1
  fi
  if [ "$tar" -eq 1 ]; then
    (
      set -o pipefail
      tar -cf - "$2" | __pv | $1 | __pv >"$target"
    )
  else
    (
      set -o pipefail
      $1 "$2" | __pv >"$target"
    )
  fi
}

compress_split() {
  # shellcheck disable=2016
  local msg='Usage: compress_split [-h|--help]
compress_split [-t|--tar] [-n|--no-tar] [-p|--pad PAD] [-b BYTES|--bytes BYTES] COMMAND SOURCE [TARGET]
If TARGET is not provided and PAD is provided, SOURCE concatenating PAD is used.
No tar is used by default.
If BYTES is not provided, $SPLIT_SIZE is used if set and 4000M is used otherwise.'
  local bytes
  local pad=''
  local target=''
  if [ -n "${SPLIT_SIZE:-}" ]; then
    bytes="$SPLIT_SIZE"
  else
    bytes="4000M"
  fi
  local tar=0
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -h | --help)
        echo "$msg"
        return 0
        ;;
      -t | --tar)
        tar=1
        shift
        ;;
      -n | --no-tar)
        tar=0
        shift
        ;;
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          echo "$msg" >&2
          return 1
        fi
        pad="$2"
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          echo "$msg" >&2
          return 1
        fi
        bytes="$2"
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  if [ "$#" -eq 3 ]; then
    target="$3"
  elif [ "$#" -eq 2 ] && [ -n "$pad" ]; then
    target="${2}${pad}"
  else
    echo "$msg" >&2
    return 1
  fi
  if [ "$tar" -eq 1 ]; then
    (
      set -o pipefail
      tar -cf - "$2" | __pv | $1 | __pv | split -b "$bytes" -d -a 3 - "$target.part."
    )
  else
    (
      set -o pipefail
      $1 "$2" | __pv | split -b "$bytes" -d -a 3 - "$target.part."
    )
  fi
}

bz2_single() {
  compress_single --tar --pad '.tar.bz2' 'bzip2 -9 -c' "$@"
}

gz_single() {
  compress_single --tar --pad '.tar.gz' 'gzip -9 -c' "$@"
}

xz_single() {
  compress_single --tar --pad '.tar.xz' 'xz -9 -c' "$@"
}

zst_single() {
  compress_single --tar --pad '.tar.zst' 'zstd --ultra -22 -c' "$@"
}

tar_single() {
  compress_single --no-tar --pad '.tar' 'tar -cf -' "$@"
}

zip_single() {
  compress_single --no-tar --pad '.zip' 'zip -r -9 -' "$@"
}

7z_single() {
  compress_single --no-tar --pad '.7z' '7z a -mx=9 -so' "$@"
}

7z_non_solid_single() {
  compress_single --no-tar --pad '.7z' '7z a -mx=9 -ms=off -so' "$@"
}

bz2_split() {
  compress_split --tar --pad '.tar.bz2' 'bzip2 -9 -c' "$@"
}

gz_split() {
  compress_split --tar --pad '.tar.gz' 'gzip -9 -c' "$@"
}

xz_split() {
  compress_split --tar --pad '.tar.xz' 'xz -9 -c' "$@"
}

zst_split() {
  compress_split --tar --pad '.tar.zst' 'zstd --ultra -22 -c' "$@"
}

tar_split() {
  compress_split --no-tar --pad '.tar' 'tar -cf -' "$@"
}

zip_split() {
  compress_split --no-tar --pad '.zip' 'zip -r -9 -' "$@"
}

7z_split() {
  compress_split --no-tar --pad '.7z' '7z a -mx=9 -so' "$@"
}

7z_non_solid_split() {
  compress_split --no-tar --pad '.7z' '7z a -mx=9 -ms=off -so' "$@"
}

bz2_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --tar --pad '.tar.bz2' 'bzip2 -9 -c' "$f" "$@" && rm -- "$f"
    done
  )
}

gz_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --tar --pad '.tar.gz' 'gzip -9 -c' "$f" "$@" && rm -- "$f"
    done
  )
}

xz_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --tar --pad '.tar.xz' 'xz -9 -c' "$f" "$@" && rm -- "$f"
    done
  )
}

zst_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --tar --pad '.tar.zst' 'zstd --ultra -22 -c' "$f" "$@" && rm -- "$f"
    done
  )
}

tar_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --no-tar --pad '.tar' 'tar -cf -' "$f" "$@" && rm -- "$f"
    done
  )
}

zip_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --no-tar --pad '.zip' 'zip -r -9 -' "$f" "$@" && rm -- "$f"
    done
  )
}

7z_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --no-tar --pad '.7z' '7z a -mx=9 -so' "$f" "$@" && rm -- "$f"
    done
  )
}

7z_non_solid_single_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_single --no-tar --pad '.7z' '7z a -mx=9 -ms=off -so' "$f" "$@" && rm -- "$f"
    done
  )
}

bz2_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --tar --pad '.tar.bz2' 'bzip2 -9' "$f" "$@" && rm -- "$f"
    done
  )
}

gz_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --tar --pad '.tar.gz' 'gzip -9' "$f" "$@" && rm -- "$f"
    done
  )
}

xz_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --tar --pad '.tar.xz' 'xz -9' "$f" "$@" && rm -- "$f"
    done
  )
}

zst_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --tar --pad '.tar.zst' 'zstd --ultra -22 -c' "$f" "$@" && rm -- "$f"
    done
  )
}

tar_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --no-tar --pad '.tar' 'tar -cf -' "$f" "$@" && rm -- "$f"
    done
  )
}

zip_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --no-tar --pad '.zip' 'zip -r -9 -' "$f" "$@" && rm -- "$f"
    done
  )
}

7z_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --no-tar --pad '.7z' '7z a -mx=9 -so' "$f" "$@" && rm -- "$f"
    done
  )
}

7z_non_solid_split_all() {
  (
    shopt -s nullglob
    for f in *; do
      compress_split --no-tar --pad '.7z' '7z a -mx=9 -ms=off -so' "$f" "$@" && rm -- "$f"
    done
  )
}

extract_all_and() {
  local erar=0
  local ezip=0
  local e7z=0
  local etargz=0
  local etgz=0
  local egz=0
  local etarbz2=0
  local etbz2=0
  local ebz2=0
  local etarxz=0
  local etxz=0
  local exz=0
  local etarzst=0
  local etzst=0
  local ezst=0
  while [ "$#" -gt 0 ]; do
    case "$1" in
      --erar | --exclude-rar)
        erar=1
        shift
        ;;
      --ezip | --exclude-zip)
        ezip=1
        shift
        ;;
      --e7z | --exclude-7z)
        e7z=1
        shift
        ;;
      --etargz | --exclude-targz)
        etargz=1
        shift
        ;;
      --etgz | --exclude-tgz)
        etgz=1
        shift
        ;;
      --egz | --exclude-gz)
        egz=1
        shift
        ;;
      --etarbz2 | --exclude-tarbz2)
        etarbz2=1
        shift
        ;;
      --etbz2 | --exclude-tbz2)
        etbz2=1
        shift
        ;;
      --ebz2 | --exclude-bz2)
        ebz2=1
        shift
        ;;
      --etarxz | --exclude-tarxz)
        etarxz=1
        shift
        ;;
      --etxz | --exclude-txz)
        etxz=1
        shift
        ;;
      --exz | --exclude-xz)
        exz=1
        shift
        ;;
      --etarzst | --exclude-tarzst)
        etarzst=1
        shift
        ;;
      --etzst | --exclude-tzst)
        etzst=1
        shift
        ;;
      --ezst | --exclude-zst)
        ezst=1
        shift
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  [ "$#" -lt 2 ] && return 1
  (
    shopt -s globstar nullglob
    for f in "$1" "$1"/**/*; do
      [[ -f $f ]] || continue
      local file=${f##*/}
      local dir=${f%/*}
      [[ $dir == "$f" ]] && dir=.
      local un=''
      local -a cmd=()
      case $file in
        *.rar)
          [ "$erar" -eq 1 ] && continue
          un=${file%.rar}
          cmd=(unrar x)
          ;;
        *.zip)
          [ "$ezip" -eq 1 ] && continue
          un=${file%.zip}
          cmd=(unzip)
          ;;
        *.7z)
          [ "$e7z" -eq 1 ] && continue
          un=${file%.7z}
          cmd=(7z x)
          ;;
        *.tar.gz)
          [ "$etargz" -eq 1 ] && continue
          un=${file%.tar.gz}
          cmd=(tar -xzf)
          ;;
        *.tgz)
          [ "$etgz" -eq 1 ] && continue
          un=${file%.tgz}
          cmd=(tar -xzf)
          ;;
        *.gz)
          [ "$egz" -eq 1 ] && continue
          un=${file%.gz}
          cmd=(gzip -d)
          ;;
        *.tar.bz2)
          [ "$etarbz2" -eq 1 ] && continue
          un=${file%.tar.bz2}
          cmd=(tar -xjf)
          ;;
        *.tbz2)
          [ "$etbz2" -eq 1 ] && continue
          un=${file%.tbz2}
          cmd=(tar -xjf)
          ;;
        *.bz2)
          [ "$ebz2" -eq 1 ] && continue
          un=${file%.bz2}
          cmd=(bzip2 -d)
          ;;
        *.tar.xz)
          [ "$etarxz" -eq 1 ] && continue
          un=${file%.tar.xz}
          cmd=(tar -xJf)
          ;;
        *.txz)
          [ "$etxz" -eq 1 ] && continue
          un=${file%.txz}
          cmd=(tar -xJf)
          ;;
        *.xz)
          [ "$exz" -eq 1 ] && continue
          un=${file%.xz}
          cmd=(xz -d)
          ;;
        *.tar.zst)
          [ "$etarzst" -eq 1 ] && continue
          un=${file%.tar.zst}
          cmd=(tar -xf --zstd)
          ;;
        *.tzst)
          [ "$etzst" -eq 1 ] && continue
          un=${file%.tzst}
          cmd=(tar -xf --zstd)
          ;;
        *.zst)
          [ "$ezst" -eq 1 ] && continue
          un=${file%.zst}
          cmd=(zst -d)
          ;;
        *)
          continue
          ;;
      esac
      (
        cd "$dir" &&
          "${cmd[@]}" "$file" &&
          rm -- "$file" &&
          "${@:2}" "$un" &&
          rm -rf -- "$un"
      )
    done
  )
}

extract_all() {
  extract_all_and "$@" false
}

extract_all_and_bz2() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" bz2_single "${args[@]}"
}

extract_all_and_gz() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" gz_single "${args[@]}"
}

extract_all_and_xz() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" xz_single "${args[@]}"
}

extract_all_and_zst() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" zst_single "${args[@]}"
}

extract_all_and_tar() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" tar_single "${args[@]}"
}

extract_all_and_zip() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" zip_single "${args[@]}"
}

extract_all_and_7z() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" 7z_single "${args[@]}"
}

extract_all_and_7z_non_solid() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" 7z_non_solid_single "${args[@]}"
}

extract_all_and_bz2_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" bz2_split "${args[@]}"
}

extract_all_and_gz_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" gz_split "${args[@]}"
}

extract_all_and_xz_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" xz_split "${args[@]}"
}

extract_all_and_tar_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" tar_split "${args[@]}"
}

extract_all_and_zip_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      -b | --bytes)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" zip_split "${args[@]}"
}

extract_all_and_7z_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" 7z_split "${args[@]}"
}

extract_all_and_7z_non_solid_split() {
  local -a args=()
  while [ "$#" -gt 0 ]; do
    case "$1" in
      -p | --pad)
        if [ "$#" -lt 2 ]; then
          return 1
        fi
        args+=("$1" "$2")
        shift 2
        ;;
      --)
        shift
        break
        ;;
      *)
        break
        ;;
    esac
  done
  extract_all_and "$@" 7z_non_solid_split "${args[@]}"
}
View original on lemmy.world
2

No replies yet

No comments on the original post yet.
Added zstd, 7z, and 7z non-solid support to compression and extraction bash functions | Spyke