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.

Revisions

  1. soraxas revised this gist Sep 1, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ function extract {
    fi

    case "${file%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar|*.tar.zst)
    tar xvf "$file" $tar_args
    ;;
    *.tar.zst)
  2. soraxas revised this gist Sep 1, 2023. 1 changed file with 105 additions and 43 deletions.
    148 changes: 105 additions & 43 deletions compress
    Original file line number Diff line number Diff line change
    @@ -2,36 +2,100 @@

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"
    DEFAULT_FNAME=compressed_output
    DEFAULT_FORMAT=tar.gz
    SCRIPT="$(basename $0)"

    function extract {
    if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: $SCRIPT <target.(zip|rar|tar.*|7z)> <file1> [file2] ..."
    echo "Usage: $SCRIPT <file1>"
    echo ""
    echo "Use corresponding compression software based on output filename"
    echo "If only one input is given, .$DEFAULT_FORMAT format is assumed, and named as file1.$DEFAULT_FORMAT"
    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="$1"
    #if [ -f "$target" ] ; then
    # echo "'$target' - file already exist!"
    # return 1
    #fi
    if [ -z "$2" ]; then
    # echo "Require at least one file to compress!"
    # return 1
    # remove trailing slash
    target="${target%/}".$DEFAULT_FORMAT
    else
    shift
    fi

    case "${target%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    tar cavf "$target" $@
    ;;
    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" ;;
    @@ -40,15 +104,15 @@ function extract {
    # unzip ./"$target" $unzip_args
    # ;;
    # *.z) uncompress ./"$target" ;;
    *.7z)
    7z a "$target" $@
    ;;
    *.zip)
    zip -r "$target" $@
    ;;
    *.rar)
    rar a "$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" ;;
    @@ -59,14 +123,12 @@ function extract {
    # *.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
    fi
    *)
    echo "$SCRIPT: '$target' - unknown archive method"
    return 1
    ;;
    esac
    }

    IFS=$SAVEIFS

    extract $@
    compress $@
  3. soraxas revised this gist Dec 21, 2022. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions extract
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,18 @@ 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>"
    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."
    @@ -29,12 +37,22 @@ function extract {
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    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)
    unzip ./"$file" $unzip_args
    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)
  4. soraxas revised this gist Nov 23, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ function extract {
    fi

    case "${file%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar|*.tar.zst)
    tar xvf "$file" $tar_args
    ;;
    *.lzma) unlzma ./"$file" ;;
  5. soraxas revised this gist Feb 27, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions extract
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ function extract {
    *.zpaq) zpaq x ./"$file" ;;
    *.arc) arc e ./"$file" ;;
    *.cso) ciso 0 ./"$file" ./"$file.iso" && \
    extract $file.iso && \rm -f $file ;;
    extract "$file.iso" && \rm -f "$file" ;;
    *)
    echo "$script: '$file' - unknown archive method"
    return 1
    @@ -57,4 +57,4 @@ function extract {

    IFS=$SAVEIFS

    extract $@
    extract "$@"
  6. soraxas revised this gist Mar 12, 2021. 1 changed file with 17 additions and 13 deletions.
    30 changes: 17 additions & 13 deletions compress
    Original file line number Diff line number Diff line change
    @@ -2,27 +2,31 @@

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"
    script="$(basename $0)"
    DEFAULT_FORMAT=tar.gz
    SCRIPT="$(basename $0)"

    function extract {
    if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: $script <target>.<zip|rar|tar.*|7z> <file1> [file2] ..."
    echo "Usage: $SCRIPT <target.(zip|rar|tar.*|7z)> <file1> [file2] ..."
    echo "Usage: $SCRIPT <file1>"
    echo ""
    echo "Use corresponding compression software based on output filename"
    echo "If only one input is given, .$DEFAULT_FORMAT format is assumed, and named as file1.$DEFAULT_FORMAT"
    else
    target="$1"
    shift
    if [ -f "$target" ] ; then
    echo "'$target' - file already exist!"
    return 1
    #if [ -f "$target" ] ; then
    # echo "'$target' - file already exist!"
    # return 1
    #fi
    if [ -z "$2" ]; then
    # echo "Require at least one file to compress!"
    # return 1
    # remove trailing slash
    target="${target%/}".$DEFAULT_FORMAT
    else
    shift
    fi
    if [ -z "$1" ]; then
    echo "Require at least one file to compress!"
    return 1
    fi
    echo $@
    #exit

    case "${target%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    @@ -56,7 +60,7 @@ function extract {
    # *.cso) ciso 0 ./"$target" ./"$target.iso" && \
    # extract $target.iso && \rm -f $target ;;
    *)
    echo "$script: '$target' - unknown archive method"
    echo "$SCRIPT: '$target' - unknown archive method"
    return 1
    ;;
    esac
  7. soraxas revised this gist Mar 12, 2021. 1 changed file with 68 additions and 0 deletions.
    68 changes: 68 additions & 0 deletions compress
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/bin/bash

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"
    script="$(basename $0)"

    function extract {
    if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: $script <target>.<zip|rar|tar.*|7z> <file1> [file2] ..."
    echo ""
    echo "Use corresponding compression software based on output filename"
    else
    target="$1"
    shift
    if [ -f "$target" ] ; then
    echo "'$target' - file already exist!"
    return 1
    fi
    if [ -z "$1" ]; then
    echo "Require at least one file to compress!"
    return 1
    fi
    echo $@
    #exit

    case "${target%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    tar cavf "$target" $@
    ;;
    # *.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" $@
    ;;
    *.zip)
    zip -r "$target" $@
    ;;
    *.rar)
    rar a "$target" $@
    ;;
    # *.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
    fi
    }

    IFS=$SAVEIFS

    extract $@
  8. soraxas revised this gist Mar 12, 2021. 1 changed file with 48 additions and 39 deletions.
    87 changes: 48 additions & 39 deletions extract
    Original file line number Diff line number Diff line change
    @@ -4,48 +4,57 @@

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"
    script="$(basename $0)"

    function extract {
    if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
    echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
    else
    for n in "$@"
    do
    if [ -f "$n" ] ; then
    case "${n%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    tar xvf "$n" ;;
    *.lzma) unlzma ./"$n" ;;
    *.bz2) bunzip2 ./"$n" ;;
    *.cbr|*.rar) unrar x -ad ./"$n" ;;
    *.gz) gunzip ./"$n" ;;
    *.cbz|*.epub|*.zip) unzip ./"$n" ;;
    *.z) uncompress ./"$n" ;;
    *.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
    7z x ./"$n" ;;
    *.xz) unxz ./"$n" ;;
    *.exe) cabextract ./"$n" ;;
    *.cpio) cpio -id < ./"$n" ;;
    *.cba|*.ace) unace x ./"$n" ;;
    *.zpaq) zpaq x ./"$n" ;;
    *.arc) arc e ./"$n" ;;
    *.cso) ciso 0 ./"$n" ./"$n.iso" && \
    extract $n.iso && \rm -f $n ;;
    *)
    echo "extract: '$n' - unknown archive method"
    return 1
    ;;
    esac
    else
    echo "'$n' - file does not exist"
    return 1
    fi
    done
    fi
    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>"
    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 xvf "$file" $tar_args
    ;;
    *.lzma) unlzma ./"$file" ;;
    *.bz2) bunzip2 ./"$file" ;;
    *.cbr|*.rar) unrar x -ad ./"$file" ;;
    *.gz) gunzip ./"$file" ;;
    *.cbz|*.epub|*.zip)
    unzip ./"$file" $unzip_args
    ;;
    *.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 $@
    extract $@
  9. soraxas revised this gist May 7, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions decompress
  10. soraxas revised this gist May 7, 2020. 1 changed file with 0 additions and 0 deletions.
    Empty file modified extract
    100644 → 100755
    Empty file.
  11. soraxas revised this gist May 7, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions extract
    Original file line number Diff line number Diff line change
    @@ -48,5 +48,4 @@ fi

    IFS=$SAVEIFS

    extract $@

    extract $@
  12. soraxas revised this gist May 7, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # from https://github.com/xvoland/Extract
    # function Extract for common file formats
    # function Extract for common file format

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"
  13. soraxas revised this gist May 7, 2020. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions extract
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,6 @@
    # from https://github.com/xvoland/Extract
    # function Extract for common file formats

    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")

    #!/bin/bash
    # function Extract for common file formats

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"

  14. soraxas revised this gist May 7, 2020. 1 changed file with 58 additions and 1 deletion.
    59 changes: 58 additions & 1 deletion extract
    Original file line number Diff line number Diff line change
    @@ -1 +1,58 @@
    ‎‎​
    #!/bin/bash
    # from https://github.com/xvoland/Extract
    # function Extract for common file formats

    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")

    #!/bin/bash
    # function Extract for common file formats

    SAVEIFS=$IFS
    IFS="$(printf '\n\t')"

    function extract {
    if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
    echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
    else
    for n in "$@"
    do
    if [ -f "$n" ] ; then
    case "${n%,}" in
    *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
    tar xvf "$n" ;;
    *.lzma) unlzma ./"$n" ;;
    *.bz2) bunzip2 ./"$n" ;;
    *.cbr|*.rar) unrar x -ad ./"$n" ;;
    *.gz) gunzip ./"$n" ;;
    *.cbz|*.epub|*.zip) unzip ./"$n" ;;
    *.z) uncompress ./"$n" ;;
    *.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
    7z x ./"$n" ;;
    *.xz) unxz ./"$n" ;;
    *.exe) cabextract ./"$n" ;;
    *.cpio) cpio -id < ./"$n" ;;
    *.cba|*.ace) unace x ./"$n" ;;
    *.zpaq) zpaq x ./"$n" ;;
    *.arc) arc e ./"$n" ;;
    *.cso) ciso 0 ./"$n" ./"$n.iso" && \
    extract $n.iso && \rm -f $n ;;
    *)
    echo "extract: '$n' - unknown archive method"
    return 1
    ;;
    esac
    else
    echo "'$n' - file does not exist"
    return 1
    fi
    done
    fi
    }

    IFS=$SAVEIFS

    extract $@

  15. soraxas created this gist May 7, 2020.
    1 change: 1 addition & 0 deletions extract
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​