]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/extract/extract.plugin.zsh
1112dd52f289ed34b3342750fca04ecb9d8d216f
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / extract / extract.plugin.zsh
1 alias x=extract
2
3 extract() {
4   setopt localoptions noautopushd
5
6   if (( $# == 0 )); then
7     cat >&2 <<'EOF'
8 Usage: extract [-option] [file ...]
9
10 Options:
11     -r, --remove    Remove archive after unpacking.
12 EOF
13   fi
14
15   local remove_archive=1
16   if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
17     remove_archive=0
18     shift
19   fi
20
21   local pwd="$PWD"
22   while (( $# > 0 )); do
23     if [[ ! -f "$1" ]]; then
24       echo "extract: '$1' is not a valid file" >&2
25       shift
26       continue
27     fi
28
29     local success=0
30     local extract_dir="${1:t:r}"
31     local file="$1" full_path="${1:A}"
32     case "${file:l}" in
33       (*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$file" | tar xv } || tar zxvf "$file" ;;
34       (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
35       (*.tar.xz|*.txz)
36         tar --xz --help &> /dev/null \
37         && tar --xz -xvf "$file" \
38         || xzcat "$file" | tar xvf - ;;
39       (*.tar.zma|*.tlz)
40         tar --lzma --help &> /dev/null \
41         && tar --lzma -xvf "$file" \
42         || lzcat "$file" | tar xvf - ;;
43       (*.tar.zst|*.tzst)
44         tar --zstd --help &> /dev/null \
45         && tar --zstd -xvf "$file" \
46         || zstdcat "$file" | tar xvf - ;;
47       (*.tar) tar xvf "$file" ;;
48       (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
49       (*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
50       (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
51       (*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
52       (*.bz2) bunzip2 "$file" ;;
53       (*.xz) unxz "$file" ;;
54       (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
55       (*.lz4) lz4 -d "$file" ;;
56       (*.lzma) unlzma "$file" ;;
57       (*.z) uncompress "$file" ;;
58       (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
59       (*.rar) unrar x -ad "$file" ;;
60       (*.rpm)
61         command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
62         && rpm2cpio "$full_path" | cpio --quiet -id ;;
63       (*.7z) 7za x "$file" ;;
64       (*.deb)
65         command mkdir -p "$extract_dir/control" "$extract_dir/data"
66         builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
67         builtin cd -q control; extract ../control.tar.*
68         builtin cd -q ../data; extract ../data.tar.*
69         builtin cd -q ..; command rm *.tar.* debian-binary ;;
70       (*.zst) unzstd "$file" ;;
71       (*.cab) cabextract -d "$extract_dir" "$file" ;;
72       (*.cpio) cpio -idmvF "$file" ;;
73       (*)
74         echo "extract: '$file' cannot be extracted" >&2
75         success=1 ;;
76     esac
77
78     (( success = success > 0 ? success : $? ))
79     (( success == 0 && remove_archive == 0 )) && rm "$full_path"
80     shift
81
82     # Go back to original working directory in case we ran cd previously
83     builtin cd -q "$pwd"
84   done
85 }