2010-01-31 13:13:20 +01:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
#
|
|
|
|
|
# xzize, 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.
|
|
|
|
|
#
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# This script recompresses to XZ (with maximum compression level) a
|
|
|
|
|
# GZip (.gz, .tgz), BZip2 (.bz2, .tbz, .tb2), LZMA (.lzma, .tlz), and
|
|
|
|
|
# Lempel-Ziv (.Z, .taz) compressed file. The original file is keeped.
|
2010-01-31 13:13:20 +01:00
|
|
|
|
# If the file extension does not correspond to a known compression
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# format, the file is compressed to XZ. In that case, the original
|
2010-01-31 13:13:20 +01:00
|
|
|
|
# file is removed.
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# If the file is already XZ compressed (.xz, .txz), nothing is done.
|
2010-01-31 13:13:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# Destination file suffix when source file name suffix is a
|
|
|
|
|
# 3-letter compressed tar suffix :
|
|
|
|
|
TAR_XZ_EXT=txz
|
|
|
|
|
|
2010-01-31 13:13:20 +01:00
|
|
|
|
|
|
|
|
|
test_extension()
|
|
|
|
|
{
|
|
|
|
|
local - FILE EXTENSION BASE
|
|
|
|
|
FILE="$1"
|
|
|
|
|
EXTENSION="$2"
|
2010-01-31 16:14:45 +01:00
|
|
|
|
NEWEXTENSION=xz
|
|
|
|
|
if [ $# -eq 3 ] ; then
|
|
|
|
|
NEWEXTENSION="$3"
|
|
|
|
|
fi
|
|
|
|
|
|
2010-01-31 13:13:20 +01:00
|
|
|
|
BASE="`basename "$FILE" ."$EXTENSION"`"
|
|
|
|
|
if [ "$BASE" != "$FILE" ] ; then
|
2010-01-31 16:14:45 +01:00
|
|
|
|
DEST="$BASE".$NEWEXTENSION
|
2010-01-31 13:13:20 +01:00
|
|
|
|
return 0
|
|
|
|
|
fi
|
2010-01-31 16:14:45 +01:00
|
|
|
|
|
2010-01-31 13:13:20 +01:00
|
|
|
|
DEST="$FILE"
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do_recompress()
|
|
|
|
|
{
|
|
|
|
|
if [ -e "$DEST" ] ; then
|
|
|
|
|
echo "Skipping source file « $SOURCE »: destination file « $DEST » already exists!" >&2
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
echo "Recompressing « $SOURCE » to « $DEST »…"
|
2010-01-31 15:27:39 +01:00
|
|
|
|
$ACTION -c "$SOURCE" | xz -9 >"$DEST"
|
2010-01-31 13:13:20 +01:00
|
|
|
|
return $?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Verify number of arguments ##
|
|
|
|
|
|
|
|
|
|
if [ $# -lt 1 ] ; then
|
2010-01-31 15:27:39 +01:00
|
|
|
|
echo "$0 requires at least one argument!" >&2
|
2010-01-31 13:13:20 +01:00
|
|
|
|
exit 2
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Process files ##
|
|
|
|
|
|
|
|
|
|
for SOURCE in "$@" ; do
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# Does source file exist?
|
|
|
|
|
if [ ! -e "$SOURCE" ] ; then
|
|
|
|
|
echo "Skipping source file « $SOURCE »: does not exist!" >&2
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Does source file is regular?
|
2010-01-31 15:27:39 +01:00
|
|
|
|
if [ ! -f "$SOURCE" ] ; then
|
|
|
|
|
echo "Skipping source file « $SOURCE »: is not a regular file!" >&2
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2010-01-31 13:13:20 +01:00
|
|
|
|
|
|
|
|
|
# GZip
|
2010-01-31 16:14:45 +01:00
|
|
|
|
if test_extension "$SOURCE" gz ; then
|
|
|
|
|
ACTION=gunzip
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# GZipped Tar (.tgz)
|
|
|
|
|
if test_extension "$SOURCE" tgz $TAR_XZ_EXT ; then
|
2010-01-31 13:13:20 +01:00
|
|
|
|
ACTION=gunzip
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# BZip2
|
2010-01-31 16:14:45 +01:00
|
|
|
|
if test_extension "$SOURCE" bz2 ; 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
|
2010-01-31 13:13:20 +01:00
|
|
|
|
ACTION=bunzip2
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# LZMA
|
2010-01-31 16:14:45 +01:00
|
|
|
|
if test_extension "$SOURCE" lzma ; then
|
|
|
|
|
ACTION=unlzma
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# LZMA compressed Tar (.tlz)
|
|
|
|
|
if test_extension "$SOURCE" tlz $TAR_XZ_EXT ; then
|
2010-01-31 13:13:20 +01:00
|
|
|
|
ACTION=unlzma
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Compress
|
2010-01-31 16:14:45 +01:00
|
|
|
|
if test_extension "$SOURCE" Z ; then
|
|
|
|
|
ACTION=uncompress
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Compressed Tar (.taz)
|
|
|
|
|
if test_extension "$SOURCE" taz $TAR_XZ_EXT ; then
|
2010-01-31 13:13:20 +01:00
|
|
|
|
ACTION=uncompress
|
|
|
|
|
do_recompress
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# XZ
|
|
|
|
|
if test_extension "$SOURCE" xz ; then
|
2010-01-31 15:27:39 +01:00
|
|
|
|
echo "Skipping source file « $DEST »: has already .xz suffix!" >&2
|
2010-01-31 13:13:20 +01:00
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2010-01-31 16:14:45 +01:00
|
|
|
|
# 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
|
|
|
|
|
|
2010-01-31 13:13:20 +01:00
|
|
|
|
# Uncompressed file: fall back to simple compression
|
2010-01-31 15:27:39 +01:00
|
|
|
|
echo "Compressing « $SOURCE » to xz…"
|
2010-01-31 13:13:20 +01:00
|
|
|
|
xz -9 "$SOURCE"
|
|
|
|
|
|
|
|
|
|
done
|