Ticket #3290: uniunpack.sh

File uniunpack.sh, 1.3 KB (added by birdie, 4 years ago)
Line 
1#! /bin/sh
2
3# Universal Unpacker by Artem S. Tashkinov
4# v1.4.0 Oct 18 15:35 2014: total rewrite; proper gzip support; no command output :(
5# v1.4.1 Oct 26 13:20 2014: file extension normalized
6# v1.4.2 Nov 4  04:29 2014: use RAR by default, use -r- for RAR, BASH->SH
7# v1.4.3 Mar 7  22:02 2015: use UNRAR by default
8# v1.4.4 Tue 04 Nov 2019 01:18:27: prefer p7zip for zip archives
9
10test -z "$1" && echo "Need an archive name to proceed" && exit 1
11test ! -e "$1" && echo "File '$1' doesn't exist" && exit 1
12
13lower=$(echo "$1" | tr A-Z a-z)
14
15case "$lower" in
16    *.tar|*.tbz|*.txz|*.tgz|*.tar.bz2|*.tar.gz|*.tar.xz|*.tar.lz)
17        tar xf "$1"
18        ;;
19    *.rar)
20        unrar x -r- "$1"
21        ;;
22    *.7z)
23        7z x "$1"
24        ;;
25    *.bz2)
26        bzip2 -kd "$1"
27        ;;
28    *.gz)
29        gunzip -k "$1"
30        ;;
31    *.xz)
32        xz -dk "$1"
33        ;;
34    *.zip)
35        if 7z &> /dev/null; then
36            7z x "$1"
37        else
38            unzip "$1"
39        fi
40        ;;
41    *.cpio)
42        cpio -i -m -d -F "$1"
43        ;;
44    *)
45        echo "The archive type for '$1' is unknown"
46        echo -n "Would you like to try unpacking the archive using 7z (y/n)? "
47        read answer
48        test "$answer" = "Y" -o "$answer" = "y" && 7z x "$1"
49        ;;
50esac
51
52res=$?
53test "$res" -ne 0 && echo "WARNING: Unpacking failed!"
54exit "$res"