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 # Latest change: Sam Nov 25 21:37:22 CET 2006 [mika]
8 ################################################################################
11 autoload -U colors && colors
16 alarmcol=$fg_bold[red]
17 defcol=$fg[default]$bg[default]
19 col2=$fg_bold[magenta]
23 usage() { print "Usage: $FILENAME file_to_extract" && exit ; }
24 (( $ARGC != 1 )) && usage
25 [[ $ARGV == "-h" || $ARGV == "--help" ]] && usage
27 FILEPRG=$( which file )
28 [[ $( tar --version 3>&1 ) != *(GNU|star)* ]] && echo "sorry, GNU tar or star is needed" && exit
29 test -z $FILEPRG && echo $0 needs a program named file to detect the filetype && exit
32 Really decompress $alarmcol${file}$defcol? Press y or Y for YES
33 or any other key for NO $defcol
37 ${col1}Okay, I will not uncompress the file $col2${file}$defcol as requested.
39 decision="read -q '?$qprompt'"
41 local -A filetype; filetype=(\
42 bz2cpio "cpio archive *bzip2*"
43 cpiogz "cpio archive *gzip*"
44 tarbz2 "*tar archive*bzip2*"
45 targz "*tar archive*gzip*"
46 tarz "*POSIX tar archive \(compress\'d data 16 bits\)*"
47 bz2 "*\(bzip2 compressed data*"
50 7zip "*7-zip archive data*"
51 arj "ARJ archive data*"
52 zip "*Zip archive data*"
53 Z "*compress'd data 16 bits*"
54 deb "Debian binary package*"
55 uu "*uuencoded*xxencoded*"
56 lzo "lzop compressed data - *"
58 rar "RAR archive data*"
63 tarlist='/usr/bin/env tar -tvf' ; tarextract='/usr/bin/env tar -C $whereto -xvf'
64 tarbz2list='/usr/bin/env tar -tvjf' ; tarbz2extract='/usr/bin/env tar -C ${whereto:-$PWD} -xvjf'
65 targzlist='/usr/bin/env tar -tvzf' ; targzextract='/usr/bin/env tar -C $whereto -xvzf'
66 gzlist='/usr/bin/env gzip -lv' ; gzextract='/usr/bin/env gzip -dc'
67 tarzlist='/usr/bin/env tar -tvZf' ; tarzextract='/usr/bin/env tar -C $whereto -xvZf'
68 tarcompresslist='/usr/bin/env tar -tvZf' ; tarcompressextract='/usr/bin/env tar -$ whereto -xvZf'
69 #cpiobz2list='bzip2 -cd $file | cpio -ivt'; cpiobz2extract='bzip2 -cd $CWD/$file | cpio -idv'
70 #cpiogzlist='gzip -cd $file | cpio -tv'
71 #cpiogzextract='gzip -cd $file | cpio -ivd'
72 cpiogz='/usr/bin/env star tzvf' ; cpiogzextract='/usr/bin/env star xzvf'
73 _7ziplist='/usr/bin/env 7zr l' ; _7zipextract='/usr/bin/env 7zr e -o$whereto'
74 arjlist='/usr/bin/env arj l' ; arjextract='/usr/bin/env arj e -e $CWD/$file'
75 compresslist='/usr/bin/env uncompress -l' ; compressextract='/usr/bin/env uncompress -cv'
76 lhalist='/usr/bin/env lha -l' ; lhaextract='/usr/bin/env lha -xw=$whereto'
77 deblist='/usr/bin/env dpkg-deb -c' ; debextract='/usr/bin/env dpkg -X'
78 lzolist='/usr/bin/env lzop -l' ; lzoextract='/usr/bin/env lzop -dN -p$whereto'
79 cpiolist='/usr/bin/env cpio -t' ; cpioextract='/usr/bin/env cpio -idv'
80 rarlist='/usr/bin/env unrar lb' ; rarextract='/usr/bin/env unrar e'
81 cablist='/usr/bin/env cabextract -l' ; cabextract='/usr/bin/env cabextract -d $whereto'
82 #rpmlist='/usr/bin/env rpm2cpio #rpmextract=
83 ziplist='/usr/bin/env unzip -l' ; zipextract='/usr/bin/env unzip -d $whereto'
86 # ------------------------------------------------- functions
89 ${col2}Where should I extract to?
90 ${col4}( Non existing directories will be created,
91 completion works as do usual editing commands )$defcol
92 " whereto && test -d $whereto \
93 || builtin mkdir -p $whereto
95 # -------------------------------------------end of functions
97 (( ${+PAGER} )) || local PAGER=less
102 filetest=$( $FILEPRG -bz $file )
103 echo "This is a $col2$filetest$defcol"
107 ( ${~filetype[bz2cpio]} )
108 $=cpiobz2list |& $PAGER && eval $decision \
111 && ( cd $whereto && print "${col1}extracting to $whereto$defcol" && ${(e)=cpiobz2extract} ) \
112 || print $nothing_to_do ;;
113 ( ${~filetype[cpiogz]} )
114 ${(e)=cpiogzlist} | $PAGER && eval $decision \
117 && ( cd $whereto && print "${col1}extracting to $whereto$defcol" && ${(e)=cpiogzextract} ) \
118 || print $nothing_to_do ;;
119 ( ${~filetype[tarbz2]} )
122 $PAGER && eval $decision && put_there \
123 && ${(e)=tarbz2extract} $file \
124 || print $nothing_to_do ;;
125 ( ${~filetype[targz]} )
126 $=targzlist $file | $PAGER && eval $decision \
128 && ${(e)=targzextract} $file \
129 || print $nothing_to_do ;;
130 ( ${~filetype[tarz]} )
131 $=tarzlist $file | $PAGER && eval $decision \
133 && print "${col3}extracting to $whereto:$defcol" \
134 && ${(e)=tarzextract} $file \
135 || print $nothing_to_do ;;
136 ( ${~filetype[bz2]} )
137 bzip2 -tv $file | $PAGER && eval $decision \
139 && bzip2 -dc $file > $whereto/${file:t:r} \
140 && print "extracting to $whereto/${file:t:r}" || print $nothing_to_do ;;
142 $=gziplist $file | $PAGER && eval $decision \
144 && dest="$whereto/${file:t:r}" \
145 && gzip -dc $file > $dest && print "extracting to $dest" || print $nothing_to_do ;;
146 ( ${~filetype[tar]} )
147 $=tarlist $file | $PAGER && eval $decision \
149 && ${(e)=tarextract} $file || print $nothing_to_do ;; #ok
150 ( ${~filetype[7zip]} )
151 $=_7ziplist $file | $PAGER && eval $decision \
153 && ${(e)=_7zipextract} $file || print $nothing_to_do ;; # -o cannot stand a space, attention! #ok
154 ( ${~filetype[arj]} )
155 $=arjlist $file | $PAGER && eval $decision \
158 && ( cd $whereto && arj e -e $CWD/$file ) || print $nothing_to_do ;;
159 ( ${~filetype[zip]} )
160 ${=ziplist} $file | $PAGER && eval $decision \
162 && ${(e)=zipextract} $file || print $nothing_to_do ;; #ok
164 $=compresslist $file | $PAGER && eval $decision \
166 && dest="$whereto/${file:t:r}" \
167 && ${(e)=compressextract} $file > $dest && print "extracting to $dest" || print $nothing_to_do ;; #ok
168 ( ${~filetype[lha]} )
169 $=lhalist $file | $PAGER && eval $decision \
171 && ${(e)=lhaextract} $file || print $nothing_to_do ;; #ok
172 ( ${~filetype[rar]} )
173 $=rarlist $file | $PAGER && eval $decision \
176 && (cd $whereto && ${(e)=rarextract} $CWD/$file ) || print $nothing_to_do ;; #ok
177 ( ${~filetype[lzo]} )
178 $=lzolist $file | $PAGER && eval $decision \
180 && ${(e)=lzoextract} $file || print $nothing_to_do ;; #ok
181 ( ${~filetype[cpio]} )
182 $=cpiolist < $file | $PAGER && eval $decision \
185 && ( cd $whereto && ${(e)=cpioextract} < $CWD/$file ) || print $nothing_to_do ;; #ok
186 ( ${~filetype[deb]} )
187 ${(e)=deblist} $file | $PAGER && eval $decision \
189 && ${(e)=debextract} $file $whereto || print $nothing_to_do ;; #ok
193 && ( cd $whereto && uudecode $CWD/$file ) || print $nothing_to_do ;;
194 ( ${~filetype[cab]} )
195 $=cablist $file | $PAGER && eval $decision \
197 && ${(e)=cabextract} $file || print $nothing_to_do ;;
198 #( ${~filetype[rpm]} )
199 # temp=$( dd if=/dev/urandom bs=1 count=30 2>/dev/null | uuencode - | tr -d '[[:punct:]]' | sed '2!d' )
200 # temp=${(C)${${${(f)"$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | uuencode - 2>/dev/null)"}[2]}//[[:punct:]]}}
201 # (cd $temp && rpm2cpio $file && CPIO=*.cpio && cpio -ivt < $CPIO )\
204 # && ( cd $whereto && cpio -ivd < $temp/$file ) || print $nothing_to_do
207 echo "${alarmcol}Error. Not the expected arguments or other problem!$defcol"
208 echo "Usage: $0 file" ; exit 1 ;;
211 echo "'$file' is not a valid file"
214 ## END OF FILE #################################################################