lib/extract.sh

814 B · 35 lines · bash

1#!/usr/bin/env bash
2
3brt_extract() {
4 local archive="${1:-}"
5 local dest="${2:-.}"
6
7 if [[ -z "$archive" ]]; then
8 brt_error "Usage: brt -extract <file.zip|file.tar.gz|file.tgz> [destination]"
9 exit 1
10 fi
11
12 if [[ ! -f "$archive" ]]; then
13 brt_error "File not found: ${archive}"
14 exit 1
15 fi
16
17 mkdir -p "$dest"
18 brt_info "Extracting ${archive} to ${dest}..."
19
20 case "$archive" in
21 *.tar.gz|*.tgz) tar -xzf "$archive" -C "$dest" ;;
22 *.tar.bz2|*.tbz2) tar -xjf "$archive" -C "$dest" ;;
23 *.tar.xz|*.txz) tar -xJf "$archive" -C "$dest" ;;
24 *.tar) tar -xf "$archive" -C "$dest" ;;
25 *.zip) unzip -q "$archive" -d "$dest" ;;
26 *.gz) gunzip -k "$archive" ;;
27 *)
28 brt_error "Unsupported archive format: ${archive}"
29 exit 1
30 ;;
31 esac
32
33 brt_ok "Extracted to ${dest}"
34}
35