11/1/2016 - 3:52 PM

The ugliness of extracting with 7-zip.

The ugliness of extracting with 7-zip.

extract_file () {
  local archive="$1"
  local filename="$2"
  local output_dir="$3"
  local stdout=''

  # Extracting to a temp file, as opposed to a temp dir, is problematic in that
  # p7zip does not issue a non-zero exit status when an attempt to extract a
  # nonexistent file is made.

  stdout=$(7z x "$archive" -aoa -o"$output_dir" "$filename") || {
    local -i exit_status="$?"
    local msg="extract_file: 7z failed with exit status %s extracting file \
'%s' from archive '%s'.\n"
    printf "$msg" "$exit_status" "$filename" "$archive" >&2
    printf "%s\n" "$stdout" >&2
    return 1

  if [[ ! -e $output_dir/$filename ]]; then
    printf "extract_file: file '%s' is not present in archive '%s'.\n" \
      "$filename" "$archive" >&2
    return 2