extract.plugin.zsh 2.48 KB
Newer Older
Marc Cornellà's avatar
Marc Cornellà committed
1
alias x=extract
Sorin Ionescu's avatar
Sorin Ionescu committed
2

Marc Cornellà's avatar
Marc Cornellà committed
3
4
5
6
extract() {
	local remove_archive
	local success
	local extract_dir
Sorin Ionescu's avatar
Sorin Ionescu committed
7

Marc Cornellà's avatar
Marc Cornellà committed
8
9
10
	if (( $# == 0 )); then
		cat <<-'EOF' >&2
			Usage: extract [-option] [file ...]
Sorin Ionescu's avatar
Sorin Ionescu committed
11

Marc Cornellà's avatar
Marc Cornellà committed
12
			Options:
13
			    -r, --remove    Remove archive after unpacking.
Marc Cornellà's avatar
Marc Cornellà committed
14
15
		EOF
	fi
Sorin Ionescu's avatar
Sorin Ionescu committed
16

Marc Cornellà's avatar
Marc Cornellà committed
17
18
19
20
21
	remove_archive=1
	if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
		remove_archive=0
		shift
	fi
Sorin Ionescu's avatar
Sorin Ionescu committed
22

23
	local pwd="$PWD"
Marc Cornellà's avatar
Marc Cornellà committed
24
25
26
27
28
29
	while (( $# > 0 )); do
		if [[ ! -f "$1" ]]; then
			echo "extract: '$1' is not a valid file" >&2
			shift
			continue
		fi
Sorin Ionescu's avatar
Sorin Ionescu committed
30

Marc Cornellà's avatar
Marc Cornellà committed
31
		success=0
32
		extract_dir="${1:t:r}"
33
		local full_path="${1:A}"
34
		case "${1:l}" in
Marc Cornellà's avatar
Marc Cornellà committed
35
36
37
38
39
40
41
42
43
44
			(*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$1" | tar xv } || tar zxvf "$1" ;;
			(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
			(*.tar.xz|*.txz)
				tar --xz --help &> /dev/null \
				&& tar --xz -xvf "$1" \
				|| xzcat "$1" | tar xvf - ;;
			(*.tar.zma|*.tlz)
				tar --lzma --help &> /dev/null \
				&& tar --lzma -xvf "$1" \
				|| lzcat "$1" | tar xvf - ;;
45
46
47
48
			(*.tar.zst|*.tzst)
				tar --zstd --help &> /dev/null \
				&& tar --zstd -xvf "$1" \
				|| zstdcat "$1" | tar xvf - ;;
Marc Cornellà's avatar
Marc Cornellà committed
49
			(*.tar) tar xvf "$1" ;;
50
			(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$1" ;;
Aloxaf's avatar
Aloxaf committed
51
			(*.tar.lz4) lz4 -c -d "$1" | tar xvf - ;;
52
			(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$1" ;;
53
			(*.gz) (( $+commands[pigz] )) && pigz -dk "$1" || gunzip -k "$1" ;;
Marc Cornellà's avatar
Marc Cornellà committed
54
55
			(*.bz2) bunzip2 "$1" ;;
			(*.xz) unxz "$1" ;;
56
			(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$1" ;;
Aloxaf's avatar
Aloxaf committed
57
			(*.lz4) lz4 -d "$1" ;;
Marc Cornellà's avatar
Marc Cornellà committed
58
			(*.lzma) unlzma "$1" ;;
59
			(*.z) uncompress "$1" ;;
60
			(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d "$extract_dir" ;;
Marc Cornellà's avatar
Marc Cornellà committed
61
			(*.rar) unrar x -ad "$1" ;;
62
63
64
			(*.rpm)
				command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
				&& rpm2cpio "$full_path" | cpio --quiet -id ;;
Marc Cornellà's avatar
Marc Cornellà committed
65
66
			(*.7z) 7za x "$1" ;;
			(*.deb)
67
68
				command mkdir -p "$extract_dir/control" "$extract_dir/data"
				builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
69
70
				builtin cd -q control; extract ../control.tar.*
				builtin cd -q ../data; extract ../data.tar.*
71
				builtin cd -q ..; command rm *.tar.* debian-binary ;;
72
			(*.zst) unzstd "$1" ;;
73
			(*.cab) cabextract -d "$extract_dir" "$1" ;;
Xeonacid's avatar
Xeonacid committed
74
			(*.cpio) cpio -idmvF "$1" ;;
Marc Cornellà's avatar
Marc Cornellà committed
75
76
77
78
79
			(*)
				echo "extract: '$1' cannot be extracted" >&2
				success=1
			;;
		esac
Sorin Ionescu's avatar
Sorin Ionescu committed
80

81
82
		(( success = success > 0 ? success : $? ))
		(( success == 0 && remove_archive == 0 )) && rm "$full_path"
Marc Cornellà's avatar
Marc Cornellà committed
83
		shift
84
85
86

		# Go back to original working directory in case we ran cd previously
		builtin cd -q "$pwd"
Marc Cornellà's avatar
Marc Cornellà committed
87
	done
Sorin Ionescu's avatar
Sorin Ionescu committed
88
}