Skip to content

Instantly share code, notes, and snippets.

@soraxas
Last active September 1, 2023 05:03
Show Gist options
  • Save soraxas/ab594b0cf5c49b4e36c04249f0b7c2f7 to your computer and use it in GitHub Desktop.
Save soraxas/ab594b0cf5c49b4e36c04249f0b7c2f7 to your computer and use it in GitHub Desktop.
script:Auto extraction tool based on the file extension
#!/bin/bash
SAVEIFS=$IFS
IFS="$(printf '\n\t')"
DEFAULT_FNAME=compressed_output
DEFAULT_FORMAT=tar.gz
SCRIPT="$(basename $0)"
function help {
printf "Usage: %s [-o|--output FNAME] [-f|--format FORMAT] <file1> [file2] ... [-- [OPTS_TO_PASSTHROUGH]]\n" "$SCRIPT"
printf "Usage: %s <file1>\n" "$SCRIPT"
printf "\n"
printf "Use corresponding compression software based on output filename\n"
printf "If only one input is given, .%s format is assumed, and named as file1.%s\n" "$DEFAULT_FORMAT" "$DEFAULT_FORMAT"
exit
}
function get_ext {
_f="$(basename "$1")"
echo "${_f##*.}"
}
function get_fname {
_f="$(basename "$1")"
echo "${_f%.*}"
}
function is_supported_format {
case "$1" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) return 0 ;;
*.7z) return 0 ;;
*.zip) return 0 ;;
*.rar) return 0 ;;
esac
return 1
}
if test $# -eq 0; then
help
fi
IFS=$SAVEIFS
function compress {
filelist=()
output_format="$DEFAULT_FORMAT"
while test $# -gt 0; do
case "$1" in
-o|--output)
shift
output_fname="$1"
;;
-f|--format)
shift
output_format="$1"
;;
-h|--help)
help
;;
--verbose)
export VERBOSE="true"
;;
--)
# the rest will be passed to ffmpeg
breaknow=true
;;
*)
# set to file list
filelist+=("$1")
#set -- "$filelist" "$arg"
esac
shift
if [ -n "$breaknow" ]; then
break
fi
done
if [ -z "$output_fname" ]; then
# fallback to first file's name
output_fname="$(get_fname ${filelist[0]})"
# fallback to default
[ -z "$output_fname" ] && "$DEFAULT_FNAME"
fi
# see if output filename has extension, and file format is supported
if [ -n "$(get_ext "$output_fname")" ] && is_supported_format "$output_fname"; then
target="$output_fname"
else
target="$output_fname.$output_format"
fi
case "${target%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar cavf "$target" $@ ${filelist[@]}
;;
# *.lzma) unlzma ./"$target" ;;
# *.bz2) bunzip2 ./"$target" ;;
# *.cbr|*.rar) unrar x -ad ./"$target" ;;
# *.gz) gunzip ./"$target" ;;
# *.cbz|*.epub|*.zip)
# unzip ./"$target" $unzip_args
# ;;
# *.z) uncompress ./"$target" ;;
*.7z)
7z a "$target" $@ ${filelist[@]}
;;
*.zip)
zip -r "$target" $@ ${filelist[@]}
;;
*.rar)
rar a "$target" $@ ${filelist[@]}
;;
# *.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
# 7z x ./"$target" ;;
# *.xz) unxz ./"$target" ;;
# *.exe) cabextract ./"$target" ;;
# *.cpio) cpio -id < ./"$target" ;;
# *.cba|*.ace) unace x ./"$target" ;;
# *.zpaq) zpaq x ./"$target" ;;
# *.arc) arc e ./"$target" ;;
# *.cso) ciso 0 ./"$target" ./"$target.iso" && \
# extract $target.iso && \rm -f $target ;;
*)
echo "$SCRIPT: '$target' - unknown archive method"
return 1
;;
esac
}
compress $@
#!/bin/bash
# from https://github.com/xvoland/Extract
# function Extract for common file format
SAVEIFS=$IFS
IFS="$(printf '\n\t')"
script="$(basename $0)"
command_exists() {
if command -v "$1" >/dev/null 2>&1; then
return 0
else
return 1
fi
}
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: $script <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz|tar.zst>"
echo " $script <path/file_name_1.ext> [destination_directory]"
echo ""
echo "Note that destination_directory is only implemented on some formats."
else
file="$1"
if [ ! -f "$file" ] ; then
echo "'$file' - file does not exist"
return 1
fi
if [ -n "$2" ]; then
mkdir -p "$2" || return $?
tar_args=--directory="$2"
unzip_args="-d $2"
fi
case "${file%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar|*.tar.zst)
tar xvf "$file" $tar_args
;;
*.tar.zst)
tar --use-compress-program=unzstd -xvf "$file" $tar_args
;;
*.lzma) unlzma ./"$file" ;;
*.bz2) bunzip2 ./"$file" ;;
*.cbr|*.rar) unrar x -ad ./"$file" ;;
*.gz) gunzip ./"$file" ;;
*.cbz|*.epub|*.zip)
if command_exists 7z; then
# 7zip is preferable as unzip seems to not work with large zip file...
# (or at least 7z is able to more robustly recover from header error, in
# contrast to unzip)
7z x "$file"
else
unzip ./"$file" $unzip_args
fi
;;
*.z) uncompress ./"$file" ;;
*.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
7z x ./"$file" ;;
*.xz) unxz ./"$file" ;;
*.exe) cabextract ./"$file" ;;
*.cpio) cpio -id < ./"$file" ;;
*.cba|*.ace) unace x ./"$file" ;;
*.zpaq) zpaq x ./"$file" ;;
*.arc) arc e ./"$file" ;;
*.cso) ciso 0 ./"$file" ./"$file.iso" && \
extract "$file.iso" && \rm -f "$file" ;;
*)
echo "$script: '$file' - unknown archive method"
return 1
;;
esac
fi
}
IFS=$SAVEIFS
extract "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment