Skip to content

Instantly share code, notes, and snippets.

@zopieux
Last active August 29, 2015 14:16
Show Gist options
  • Save zopieux/c81f7afe8e60d8581d38 to your computer and use it in GitHub Desktop.
Save zopieux/c81f7afe8e60d8581d38 to your computer and use it in GitHub Desktop.
compress/uncompress/listcompress shell functions
function compress() {
this="$0"
function usage() {
echo "Usage: $this file.{zip,tgz,tbz2,xz,7z,exe} target [target ...]" >&2
}
test $# -lt 2 && ( usage; return 1 )
name="$1"
shift
case "$name" in
*.exe)
7z a -sfx "$name" $@
;;
*.zip)
7z a -tzip "$name" $@
;;
*.7z)
7z a -t7z "$name" $@
;;
*.tgz|*.tbz2|*.xz)
tar -cvf "$name" $@
;;
*)
usage; return 1
;;
esac
}
function uncompress() {
this="$0"
function usage() {
echo "Usage: $this file.{zip,tgz,tbz2,xz,7z,exe} [output dir]" >&2
}
test $# -lt 1 && ( usage; return 1 )
name="$1"
output="$2"
test -z "$output" && output="."
mime="$(file -b --mime "$name" | cut -d\; -f1)"
case "$mime" in
application/x-executable)
7z x -sfx -o"$output" "$name"
;;
application/zip)
7z x -tzip -o"$output" "$name"
;;
application/x-7z-compressed)
7z x -t7z -o"$output" "$name"
;;
application/x-gzip|application/x-bzip2|application/x-tar)
mkdir -p "$output"
tar -xvC "$output" -f "$name"
;;
*)
usage; return 1
;;
esac
}
function listcompress() {
this="$0"
function usage() {
echo "Usage: $this file.{zip,tgz,tbz2,xz,7z,exe}" >&2
}
test $# -lt 1 && ( usage; return 1 )
name="$1"
mime="$(file -b --mime "$name" | cut -d\; -f1)"
case "$mime" in
application/x-executable)
7z l -sfx "$name"
;;
application/zip)
7z l -tzip "$name"
;;
application/x-7z-compressed)
7z l -t7z "$name"
;;
application/x-gzip|application/x-bzip2|application/x-tar)
tar -tvf "$name"
;;
*)
usage; return 1
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment