diff --git a/sh/_lsarchive b/sh/_lsarchive index f5c1351..f2cee88 100755 --- a/sh/_lsarchive +++ b/sh/_lsarchive @@ -1,4 +1,3 @@ -#!/bin/zsh #compdef lsarchive #autoload @@ -11,5 +10,4 @@ _arguments \ '(-v --verbose)'{-v,--remove}'[verbose archive listing]' \ - "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|rar|7z)(-.)'" && return 0 - + "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|jar|rar|7z)(-.)'" && return 0 diff --git a/sh/_unarchive b/sh/_unarchive index 2c4caf7..90e32f1 100755 --- a/sh/_unarchive +++ b/sh/_unarchive @@ -1,4 +1,3 @@ -#!/bin/zsh #compdef unarchive #autoload @@ -11,5 +10,4 @@ _arguments \ '(-r --remove)'{-r,--remove}'[remove archive]' \ - "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|rar|7z|deb)(-.)'" && return 0 - + "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|jar|rar|7z|deb)(-.)'" && return 0 diff --git a/sh/archive b/sh/archive new file mode 100644 index 0000000..10bbad3 --- /dev/null +++ b/sh/archive @@ -0,0 +1,73 @@ +#!/usr/bin/env zsh +# +# Creates archive file +# +# Authors: +# Matt Hamilton +# + +# function archive { + +local archive_name path_to_archive _gzip_bin _bzip2_bin _xz_bin + +if (( $# < 2 )); then + cat >&2 < # +# function lsarchive { + local verbose if (( $# == 0 )); then - cat >&2 <&2 < 0 )); do - if [[ ! -s "$1" ]]; then - print "$0: file not valid: $1" >&2 - shift - continue - fi + if [[ ! -s "$1" ]]; then + print "$0: file not valid: $1" >&2 + shift + continue + fi - case "$1" in - (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;; - (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;; - (*.tar.xz|*.txz) tar --xz --help &> /dev/null \ - && tar --xz -t${verbose:+v}f "$1" \ - || xzcat "$1" | tar t${verbose:+v}f - ;; - (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \ - && tar --lzma -t${verbose:+v}f "$1" \ - || lzcat "$1" | tar x${verbose:+v}f - ;; - (*.tar) tar t${verbose:+v}f "$1" ;; - (*.zip) unzip -l${verbose:+v} "$1" ;; - (*.rar) unrar &> /dev/null \ - && unrar ${${verbose:+v}:-l} "$1" \ - || rar ${${verbose:+v}:-l} "$1" ;; - (*.7z) 7za l "$1" ;; - (*) - print "$0: cannot list: $1" >&2 - success=1 - ;; + case "$1:l" in + (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;; + (*.tar.xz|*.txz) tar --xz --help &> /dev/null \ + && tar --xz -t${verbose:+v}f "$1" \ + || xzcat "$1" | tar t${verbose:+v}f - ;; + (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \ + && tar --lzma -t${verbose:+v}f "$1" \ + || lzcat "$1" | tar x${verbose:+v}f - ;; + (*.tar) tar t${verbose:+v}f "$1" ;; + (*.zip|*.jar) unzip -l${verbose:+v} "$1" ;; + (*.rar) ( (( $+commands[unrar] )) \ + && unrar ${${verbose:+v}:-l} "$1" ) \ + || ( (( $+commands[rar] )) \ + && rar ${${verbose:+v}:-l} "$1" ) \ + || lsar ${verbose:+-l} "$1" ;; + (*.7z) 7za l "$1" ;; + (*) + print "$0: cannot list: $1" >&2 + success=1 + ;; esac shift done +# } diff --git a/sh/unarchive b/sh/unarchive index 4c2f3d0..9e38a80 100755 --- a/sh/unarchive +++ b/sh/unarchive @@ -1,4 +1,3 @@ -#!/bin/zsh # # Extracts the contents of archives. # @@ -6,13 +5,17 @@ # Sorin Ionescu # +# function unarchive { + local remove_archive local success local file_name +local file_path local extract_dir +local _gzip_bin _bzip2_bin _xz_bin if (( $# == 0 )); then - cat >&2 <&2 < 0 )); do - if [[ ! -s "$1" ]]; then - print "$0: file not valid: $1" >&2 - shift - continue - fi - - success=0 - file_name="${1:t}" - extract_dir="${file_name:r}" - case "$1" in - (*.tar.gz|*.tgz) tar xvzf "$1" ;; - (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;; - (*.tar.xz|*.txz) tar --xz --help &> /dev/null \ - && tar --xz -xvf "$1" \ - || xzcat "$1" | tar xvf - ;; - (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \ - && tar --lzma -xvf "$1" \ - || lzcat "$1" | tar xvf - ;; - (*.tar) tar xvf "$1" ;; - (*.gz) gunzip "$1" ;; - (*.bz2) bunzip2 "$1" ;; - (*.xz) unxz "$1" ;; - (*.lzma) unlzma "$1" ;; - (*.Z) uncompress "$1" ;; - (*.zip) unzip "$1" -d $extract_dir ;; - (*.rar) unrar &> /dev/null \ - && unrar e -ad "$1" \ - || rar e -ad "$1" ;; - (*.7z) 7za x "$1" ;; - (*.deb) - mkdir -p "$extract_dir/control" - mkdir -p "$extract_dir/data" - cd "$extract_dir"; ar vx "../${1}" > /dev/null - cd control; tar xzvf ../control.tar.gz - cd ../data; tar xzvf ../data.tar.gz - cd ..; rm *.tar.gz debian-binary - cd .. - ;; - (*) - print "$0: cannot extract: $1" >&2 - success=1 - ;; - esac - - (( success = $success > 0 ? $success : $? )) - (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1" + if [[ ! -s "$1" ]]; then + print "$0: file not valid: $1" >&2 shift + continue + fi + + success=0 + file_name="${1:t}" + file_path="${1:A}" + extract_dir="${file_name:r}" + case "$1:l" in + (*.tar.gz|*.tgz) tar -xvf "$1" --use-compress-program="${_gzip_bin}" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar -xvf "$1" --use-compress-program="${_bzip2_bin}" ;; + (*.tar.xz|*.txz) tar -xvf "$1" --use-compress-program="${_xz_bin}" ;; + (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \ + && tar --lzma -xvf "$1" \ + || lzcat "$1" | tar -xvf - ;; + (*.tar) tar -xvf "$1" ;; + (*.gz) gunzip "$1" ;; + (*.bz2) bunzip2 "$1" ;; + (*.xz) unxz "$1" ;; + (*.lzma) unlzma "$1" ;; + (*.Z) uncompress "$1" ;; + (*.zip|*.jar) unzip "$1" -d $extract_dir ;; + (*.rar) ( (( $+commands[unrar] )) \ + && unrar x -ad "$1" ) \ + || ( (( $+commands[rar] )) \ + && rar x -ad "$1" ) \ + || unar -d "$1" ;; + (*.7z) 7za x "$1" ;; + (*.deb) + mkdir -p "$extract_dir/control" + mkdir -p "$extract_dir/data" + cd "$extract_dir"; ar vx "${file_path}" > /dev/null + cd control; tar xvf ../control.tar.* + cd ../data; tar xvf ../data.tar.* + cd ..; rm control.tar.* data.tar.* debian-binary + cd .. + ;; + (*) + print "$0: cannot extract: $1" >&2 + success=1 + ;; + esac + + (( success = $success > 0 ? $success : $? )) + (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1" + shift done +# }