scripts/archivers/lz.sh

91 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
#
# lz.sh, Copyright © 2010 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# This script emulates and extends commands lz and uz from the mtools.
# It handles: .tar, .tar.gz, .tar.bz2, .tar.xz, .tar.lzma and .tar.Z,
# through the standards options of GNU tar.
# If the script name is "lz", the archive content is displayed. If the
# name is "uz", it is extracted.
# Note that you need GNU Tar, at least version 1.20 to support LZMA,
# and 1.22 to support XZ.
## Verify program name ##
PROGRAM="$(basename "$0")"
if [ "$PROGRAM" = "lz" ] ; then
ACTION="t"
elif [ "$PROGRAM" = "uz" ] ; then
ACTION="x"
else
printf 'Error! Program name is "%s" (not "lz" nor "uz")!\n' "$PROGRAM" >&2
exit 1
fi
## Verify number of arguments ##
if [ $# -lt 1 ] ; then
echo "$PROGRAM requires at least one argument!" >&2
exit 2
fi
## Process files ##
for F in "$@" ; do
if [ "$ACTION" = "t" ] ; then
echo "*** Listing « $F » ***" >&2
else
echo "Extracting « $F »…"
fi
# Simple Tar archive
if [ "$(basename "$F" .tar)" != "$F" ] ; then
FORMAT=""
# GZipped Tar
elif [ "$(basename "$F" .tar.gz)" != "$F" ] || \
[ "$(basename "$F" .tgz)" != "$F" ] ; then
FORMAT="--gzip"
# BZipped Tar
elif [ "$(basename "$F" .tar.bz2)" != "$F" ] || \
[ "$(basename "$F" .tbz)" != "$F" ] || \
[ "$(basename "$F" .tb2)" != "$F" ] ; then
FORMAT="--bzip"
# XZipped Tar
elif [ "$(basename "$F" .tar.xz)" != "$F" ] || \
[ "$(basename "$F" .txz)" != "$F" ] ; then
FORMAT="--xz"
# Compressed Tar
elif [ "$(basename "$F" .tar.Z)" != "$F" ] || \
[ "$(basename "$F" .taz)" != "$F" ] ; then
FORMAT="--compress"
# LZMA compressed Tar
elif [ "$(basename "$F" .tar.lzma)" != "$F" ] || \
[ "$(basename "$F" .tlz)" != "$F" ] ; then
FORMAT="--lzma"
# Unknown extension
else
printf 'Extension of file "%s" unknown! (skipping)\n' "$F" >&2
continue
fi
tar "$FORMAT" -"$ACTION"f "$F"
done
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4