[xzize] Handle short-suffix compressed tar

- Handle short-suffix compressed Tar archives.
- Test separately if a source file exists and is regular.
- Use return value of test_extension() instead of test equality of DEST
  and SOURCE.
This commit is contained in:
Matteo Cypriani 2010-01-31 16:14:45 +01:00
parent 93601c6310
commit ead9997d68
2 changed files with 70 additions and 15 deletions

View File

@ -23,7 +23,8 @@ The xzize script compresses an uncompressed file, or recompresses a
compressed file to xz (with maximum compression level). compressed file to xz (with maximum compression level).
Known compression formats are GZip (.gz), BZip2 (.bz2), LZMA (.lzma), Known compression formats are GZip (.gz), BZip2 (.bz2), LZMA (.lzma),
and Lempel-Ziv (.Z). and Lempel-Ziv (.Z). Short tar compressed suffixes are also allowed:
.tgz, .tbz, .tb2, .tlz, .taz.
In case of recompression, the original compressed file is keeped. In In case of recompression, the original compressed file is keeped. In
case of compression (i.e. when the suffix of the file does not case of compression (i.e. when the suffix of the file does not

View File

@ -8,27 +8,38 @@
# To Public License, Version 2, as published by Sam Hocevar. See # To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details. # http://sam.zoy.org/wtfpl/COPYING for more details.
# #
# This script recompresses to xz (with maximum compression level) a # This script recompresses to XZ (with maximum compression level) a
# GZip (.gz), BZip2 (.bz2), LZMA (.lzma), and Lempel-Ziv (.Z) # GZip (.gz, .tgz), BZip2 (.bz2, .tbz, .tb2), LZMA (.lzma, .tlz), and
# compressed file. The original file is keeped. # Lempel-Ziv (.Z, .taz) compressed file. The original file is keeped.
# If the file extension does not correspond to a known compression # If the file extension does not correspond to a known compression
# format, the file is compressed to xz. In that case, the original # format, the file is compressed to XZ. In that case, the original
# file is removed. # file is removed.
# If the file is already XZ compressed (.xz, .txz), nothing is done.
set -u set -u
# Destination file suffix when source file name suffix is a
# 3-letter compressed tar suffix :
TAR_XZ_EXT=txz
test_extension() test_extension()
{ {
local - FILE EXTENSION BASE local - FILE EXTENSION BASE
FILE="$1" FILE="$1"
EXTENSION="$2" EXTENSION="$2"
NEWEXTENSION=xz
if [ $# -eq 3 ] ; then
NEWEXTENSION="$3"
fi
BASE="`basename "$FILE" ."$EXTENSION"`" BASE="`basename "$FILE" ."$EXTENSION"`"
if [ "$BASE" != "$FILE" ] ; then if [ "$BASE" != "$FILE" ] ; then
DEST="$BASE".xz DEST="$BASE".$NEWEXTENSION
return 0 return 0
fi fi
DEST="$FILE" DEST="$FILE"
return 1 return 1
} }
@ -57,39 +68,76 @@ fi
## Process files ## ## Process files ##
for SOURCE in "$@" ; do for SOURCE in "$@" ; do
# Regular file? # Does source file exist?
if [ ! -e "$SOURCE" ] ; then
echo "Skipping source file « $SOURCE »: does not exist!" >&2
continue
fi
# Does source file is regular?
if [ ! -f "$SOURCE" ] ; then if [ ! -f "$SOURCE" ] ; then
echo "Skipping source file « $SOURCE »: is not a regular file!" >&2 echo "Skipping source file « $SOURCE »: is not a regular file!" >&2
continue continue
fi fi
# GZip # GZip
test_extension "$SOURCE" gz if test_extension "$SOURCE" gz ; then
if [ "$DEST" != "$SOURCE" ] ; then ACTION=gunzip
do_recompress
continue
fi
# GZipped Tar (.tgz)
if test_extension "$SOURCE" tgz $TAR_XZ_EXT ; then
ACTION=gunzip ACTION=gunzip
do_recompress do_recompress
continue continue
fi fi
# BZip2 # BZip2
test_extension "$SOURCE" bz2 if test_extension "$SOURCE" bz2 ; then
if [ "$DEST" != "$SOURCE" ] ; then ACTION=bunzip2
do_recompress
continue
fi
# BZipped Tar (.tbz)
if test_extension "$SOURCE" tbz $TAR_XZ_EXT ; then
ACTION=bunzip2
do_recompress
continue
fi
# BZipped Tar (.tb2)
if test_extension "$SOURCE" tb2 $TAR_XZ_EXT ; then
ACTION=bunzip2 ACTION=bunzip2
do_recompress do_recompress
continue continue
fi fi
# LZMA # LZMA
test_extension "$SOURCE" lzma if test_extension "$SOURCE" lzma ; then
if [ "$DEST" != "$SOURCE" ] ; then ACTION=unlzma
do_recompress
continue
fi
# LZMA compressed Tar (.tlz)
if test_extension "$SOURCE" tlz $TAR_XZ_EXT ; then
ACTION=unlzma ACTION=unlzma
do_recompress do_recompress
continue continue
fi fi
# Compress # Compress
test_extension "$SOURCE" Z if test_extension "$SOURCE" Z ; then
if [ "$DEST" != "$SOURCE" ] ; then ACTION=uncompress
do_recompress
continue
fi
# Compressed Tar (.taz)
if test_extension "$SOURCE" taz $TAR_XZ_EXT ; then
ACTION=uncompress ACTION=uncompress
do_recompress do_recompress
continue continue
@ -101,6 +149,12 @@ for SOURCE in "$@" ; do
continue continue
fi fi
# XZipped Tar (.txz)
if test_extension "$SOURCE" txz $TAR_XZ_EXT ; then
echo "Skipping source file « $DEST »: seems to be a XZipped Tar archive (.txz suffix)!" >&2
continue
fi
# Uncompressed file: fall back to simple compression # Uncompressed file: fall back to simple compression
echo "Compressing « $SOURCE » to xz…" echo "Compressing « $SOURCE » to xz…"
xz -9 "$SOURCE" xz -9 "$SOURCE"