Commit 3eceee58 authored by Glen Mével's avatar Glen Mével

`arx`: extract an archive of any common format

parent e0ae921f
#!/bin/bash
# USAGE:
# arx ARCHIVE-FILE
#
# Uncompress / extract an archive file of (almost) any archive type, according
# to its extension. Backend programs have to be available (see comments).
#
# Adapted from https://wiki.archlinux.org/index.php/Bash/Functions
#
# TODO: variant to list files without decompressing
# Note that `7z` (from package `p7zip`) also supports many formats (but, most
# notably, not XZ). However, it does not extract TAR archives automatically,
# eg. for a file `archive.tar.gz` one has to do:
# 7z x archive.tar.gz -so | tar xvf
# Yet a better alternative to deal with archives in a uniform way is the
# wonderful package `atool` which, among other tools, can compress (`apack`),
# uncompress (`aunpack`), list contents (`als`)… Of course it supports much more
# formats than this script. And, when uncompressing, it takes care of putting
# the extracted files in a directory if there is more than one.
# See https://unix.stackexchange.com/questions/73359/universal-extractors
shopt -s extglob
if [[ ! -r "$1" ]]; then
echo "$0: file is unreadable: \`$1'" >&2
exit 1
fi
case "$1" in
*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz))))) # ← here be obfuscation
c=(bsdtar xvf) ;; # from package `libarchive`
*.7z) c=(7z x) ;; # from package `p7zip`
*.Z) c=(uncompress) ;; # from package `gzip` (in group `base`)
*.a) c=(ar x) ;; # from package `binutils` (in group `base-devel`)
*.bz2) c=(bunzip2) ;; # from package `gzip` (in group `base`)
*.exe) c=(cabextract) ;; # from package `cabextract`
*.gz) c=(gunzip) ;; # from package `gzip` (in group `base`)
*.rar) c=(unrar x) ;; # from package `unrar`
*.xz) c=(unxz) ;; # from package `xz`
*.zip) c=(unzip) ;; # from package `unzip`
*)
echo "$0: unrecognized file extension: \`$1'" >&2
exit 1 ;;
esac
echo -ne '\e[33m' # set colour to yellow
echo "${c[@]}" "$1"
echo -ne '\e[0m' # reset colour
command "${c[@]}" "$1"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment