3 # Purpose: extract archives via smart frontend
4 # Authors: grml-team ( grml.org), (c) Matthias Kopfermann, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 ################################################################################
10 autoload -U colors && colors
15 alarmcol=$fg_bold[red]
16 defcol=$fg[default]$bg[default]
18 col2=$fg_bold[magenta]
22 usage() { print "Usage: $FILENAME file_to_extract" && exit ; }
23 (( $ARGC != 1 )) && usage
24 [[ $ARGV == "-h" || $ARGV == "--help" ]] && usage
26 FILEPRG=$( which file )
27 [[ $( tar --version 3>&1 ) != *(GNU|star)* ]] && echo "sorry, GNU tar or star is needed" && exit
28 test -z $FILEPRG && echo $0 needs a program named file to detect the filetype && exit
31 Really decompress $alarmcol${file}$defcol? Press y or Y for YES
32 or any other key for NO $defcol
36 ${col1}Okay, I will not uncompress the file $col2${file}$defcol as requested.
38 decision="read -q '?$qprompt'"
40 local -A filetype; filetype=(\
41 bz2cpio "cpio archive *bzip2*"
42 cpiogz "cpio archive *gzip*"
43 tarbz2 "*tar archive*bzip2*"
44 targz "*tar archive*gzip*"
45 tarz "*POSIX tar archive \(compress\'d data 16 bits\)*"
46 bz2 "*\(bzip2 compressed data*"
49 7zip "*7-zip archive data*"
50 arj "ARJ archive data*"
51 zip "*Zip archive data*"
52 Z "*compress'd data 16 bits*"
53 deb "Debian binary package*"
54 uu "*uuencoded*xxencoded*"
55 lzo "lzop compressed data - *"
57 rar "RAR archive data*"
62 tarlist='/usr/bin/env tar -tvf' ; tarextract='/usr/bin/env tar -C $whereto -xvf'
63 tarbz2list='/usr/bin/env tar -tvjf' ; tarbz2extract='/usr/bin/env tar -C ${whereto:-$PWD} -xvjf'
64 targzlist='/usr/bin/env tar -tvzf' ; targzextract='/usr/bin/env tar -C $whereto -xvzf'
65 gzlist='/usr/bin/env gzip -lv' ; gzextract='/usr/bin/env gzip -dc'
66 tarzlist='/usr/bin/env tar -tvZf' ; tarzextract='/usr/bin/env tar -C $whereto -xvZf'
67 tarcompresslist='/usr/bin/env tar -tvZf' ; tarcompressextract='/usr/bin/env tar -$ whereto -xvZf'
68 #cpiobz2list='bzip2 -cd $file | cpio -ivt'; cpiobz2extract='bzip2 -cd $CWD/$file | cpio -idv'
69 #cpiogzlist='gzip -cd $file | cpio -tv'
70 #cpiogzextract='gzip -cd $file | cpio -ivd'
71 cpiogz='/usr/bin/env star tzvf' ; cpiogzextract='/usr/bin/env star xzvf'
72 _7ziplist='/usr/bin/env 7zr l' ; _7zipextract='/usr/bin/env 7zr e -o$whereto'
73 arjlist='/usr/bin/env arj l' ; arjextract='/usr/bin/env arj e -e $CWD/$file'
74 compresslist='/usr/bin/env uncompress -l' ; compressextract='/usr/bin/env uncompress -cv'
75 lhalist='/usr/bin/env lha -l' ; lhaextract='/usr/bin/env lha -xw=$whereto'
76 deblist='/usr/bin/env dpkg-deb -c' ; debextract='/usr/bin/env dpkg -X'
77 lzolist='/usr/bin/env lzop -l' ; lzoextract='/usr/bin/env lzop -dN -p$whereto'
78 cpiolist='/usr/bin/env cpio -t' ; cpioextract='/usr/bin/env cpio -idv'
79 rarlist='/usr/bin/env unrar lb' ; rarextract='/usr/bin/env unrar e'
80 cablist='/usr/bin/env cabextract -l' ; cabextract='/usr/bin/env cabextract -d $whereto'
81 #rpmlist='/usr/bin/env rpm2cpio #rpmextract=
82 ziplist='/usr/bin/env unzip -l' ; zipextract='/usr/bin/env unzip -d $whereto'
85 # ------------------------------------------------- functions
88 ${col2}Where should I extract to?
89 ${col4}( Non existing directories will be created,
90 completion works as do usual editing commands )$defcol
91 " whereto && test -d $whereto \
92 || builtin mkdir -p $whereto
94 # -------------------------------------------end of functions
96 (( ${+PAGER} )) || local PAGER=less
101 filetest=$( $FILEPRG -bz $file )
102 echo "This is a $col2$filetest$defcol"
106 ( ${~filetype[bz2cpio]} )
107 $=cpiobz2list |& $PAGER && eval $decision \
110 && ( cd $whereto && print "${col1}extracting to $whereto$defcol" && ${(e)=cpiobz2extract} ) \
111 || print $nothing_to_do ;;
112 ( ${~filetype[cpiogz]} )
113 ${(e)=cpiogzlist} | $PAGER && eval $decision \
116 && ( cd $whereto && print "${col1}extracting to $whereto$defcol" && ${(e)=cpiogzextract} ) \
117 || print $nothing_to_do ;;
118 ( ${~filetype[tarbz2]} )
121 $PAGER && eval $decision && put_there \
122 && ${(e)=tarbz2extract} $file \
123 || print $nothing_to_do ;;
124 ( ${~filetype[targz]} )
125 $=targzlist $file | $PAGER && eval $decision \
127 && ${(e)=targzextract} $file \
128 || print $nothing_to_do ;;
129 ( ${~filetype[tarz]} )
130 $=tarzlist $file | $PAGER && eval $decision \
132 && print "${col3}extracting to $whereto:$defcol" \
133 && ${(e)=tarzextract} $file \
134 || print $nothing_to_do ;;
135 ( ${~filetype[bz2]} )
136 bzip2 -tv $file | $PAGER && eval $decision \
138 && bzip2 -dc $file > $whereto/${file:t:r} \
139 && print "extracting to $whereto/${file:t:r}" || print $nothing_to_do ;;
141 $=gziplist $file | $PAGER && eval $decision \
143 && dest="$whereto/${file:t:r}" \
144 && gzip -dc $file > $dest && print "extracting to $dest" || print $nothing_to_do ;;
145 ( ${~filetype[tar]} )
146 $=tarlist $file | $PAGER && eval $decision \
148 && ${(e)=tarextract} $file || print $nothing_to_do ;; #ok
149 ( ${~filetype[7zip]} )
150 $=_7ziplist $file | $PAGER && eval $decision \
152 && ${(e)=_7zipextract} $file || print $nothing_to_do ;; # -o cannot stand a space, attention! #ok
153 ( ${~filetype[arj]} )
154 $=arjlist $file | $PAGER && eval $decision \
157 && ( cd $whereto && arj e -e $CWD/$file ) || print $nothing_to_do ;;
158 ( ${~filetype[zip]} )
159 ${=ziplist} $file | $PAGER && eval $decision \
161 && ${(e)=zipextract} $file || print $nothing_to_do ;; #ok
163 $=compresslist $file | $PAGER && eval $decision \
165 && dest="$whereto/${file:t:r}" \
166 && ${(e)=compressextract} $file > $dest && print "extracting to $dest" || print $nothing_to_do ;; #ok
167 ( ${~filetype[lha]} )
168 $=lhalist $file | $PAGER && eval $decision \
170 && ${(e)=lhaextract} $file || print $nothing_to_do ;; #ok
171 ( ${~filetype[rar]} )
172 $=rarlist $file | $PAGER && eval $decision \
175 && (cd $whereto && ${(e)=rarextract} $CWD/$file ) || print $nothing_to_do ;; #ok
176 ( ${~filetype[lzo]} )
177 $=lzolist $file | $PAGER && eval $decision \
179 && ${(e)=lzoextract} $file || print $nothing_to_do ;; #ok
180 ( ${~filetype[cpio]} )
181 $=cpiolist < $file | $PAGER && eval $decision \
184 && ( cd $whereto && ${(e)=cpioextract} < $CWD/$file ) || print $nothing_to_do ;; #ok
185 ( ${~filetype[deb]} )
186 ${(e)=deblist} $file | $PAGER && eval $decision \
188 && ${(e)=debextract} $file $whereto || print $nothing_to_do ;; #ok
192 && ( cd $whereto && uudecode $CWD/$file ) || print $nothing_to_do ;;
193 ( ${~filetype[cab]} )
194 $=cablist $file | $PAGER && eval $decision \
196 && ${(e)=cabextract} $file || print $nothing_to_do ;;
197 #( ${~filetype[rpm]} )
198 # temp=$( dd if=/dev/urandom bs=1 count=30 2>/dev/null | uuencode - | tr -d '[[:punct:]]' | sed '2!d' )
199 # temp=${(C)${${${(f)"$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | uuencode - 2>/dev/null)"}[2]}//[[:punct:]]}}
200 # (cd $temp && rpm2cpio $file && CPIO=*.cpio && cpio -ivt < $CPIO )\
203 # && ( cd $whereto && cpio -ivd < $temp/$file ) || print $nothing_to_do
206 echo "${alarmcol}Error. Not the expected arguments or other problem!$defcol"
207 echo "Usage: $0 file" ; exit 1 ;;
210 echo "'$file' is not a valid file"
213 ## END OF FILE #################################################################