-
-
Save mina86/1341165 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -eu | |
do_zip() { | |
unzip -x "$2" | |
} | |
do_rar() { | |
unrar x "$2" | |
} | |
do_tar() { | |
tar x <$2 | |
} | |
do_tgz() { | |
gzip -d <$2 | tar x | |
} | |
do_txz() { | |
xz -d <$2 | tar x | |
} | |
do_tbz() { | |
bzip2 -d <$2 | tar x | |
} | |
do_tbz2() { | |
bzip2 -d <$2 | tar x | |
} | |
do_tlzma() { | |
lzma -d <$2 | tar x | |
} | |
err() { | |
echo "$*" >&2 | |
} | |
handle_dir() { | |
n=0 | |
f= | |
for i in * .*; do | |
if [ -e "$i" ] && [ x"$i" != x. ] && [ x"$i" != x.. ]; then | |
n=$(( $n + 1 )) | |
f=$i | |
fi | |
done | |
if [ $n -eq 0 ]; then | |
err "$file: no files extracted" | |
return 1 | |
elif [ $n -ne 1 ]; then | |
return 0 | |
elif [ -e "../$f" ]; then | |
err "$PWD/../$f: already exists" | |
err "$f: will not move the only extracted file" | |
return 0 | |
else | |
err "$f: moving to parent" | |
mv -i -- "$f" .. | |
return 1 | |
fi | |
} | |
oldpwd=$PWD | |
for file; do | |
base=${file%.*} | |
handler=${file##*.} | |
case $file in | |
*.zip|*.tar|*.rar) | |
;; | |
*.part??.rar) | |
base=${file%.part??.rar} | |
;; | |
*.tar.gz|*.tar.bz2|*.tar.lzma|*.tar.xz) | |
base=${file%.tar.*} | |
handler=t$handler | |
;; | |
*.gz|*.bz2|*.lzma|*.xz) | |
do_single_$handler | |
return $? | |
;; | |
*) | |
err "$file: could not figure out way to decompress" | |
continue | |
esac | |
if [ -e "$base" ]; then | |
err "$base: already exists" | |
continue | |
fi | |
mkdir -- "$base" || continue | |
case $file in | |
/*) | |
;; | |
*) | |
file=$oldpwd/$file | |
esac | |
cd "$base" || continue | |
if do_$handler "$handler" "$file" && handle_dir; then | |
cd "$oldpwd" | |
else | |
cd "$oldpwd" | |
rmdir -- "$base" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment