From f7c2f3aec77288624828ab47fa9dd8a5e7460729 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sun, 31 Jan 2010 13:13:20 +0100 Subject: [PATCH] [compression_utils] Add script xzize xzize is a tool to (re)compress to xz. --- compression_utils/README | 16 ++++++ compression_utils/xzize | 102 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 compression_utils/xzize diff --git a/compression_utils/README b/compression_utils/README index da63d92..b3830f5 100644 --- a/compression_utils/README +++ b/compression_utils/README @@ -1,3 +1,5 @@ +# lz and uz # + The lz 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 (and @@ -13,3 +15,17 @@ To install lz and uz, move put script in a directory in the PATH (for instance /usr/local/bin) and hard link it to uz: cp lz /usr/local/bin ln /usr/local/bin/{lz,uz} + + +# xzize # + +The xzize script compresses an uncompressed file, or recompresses a +compressed file to xz (with maximum compression level). + +Known compression formats are GZip (.gz), BZip2 (.bz2), LZMA (.lzma), +and Lempel-Ziv (.Z). + +In case of recompression, the original compressed file is keeped. In +case of compression (i.e. when the suffix of the file does not +correspond to a known compression format), the original uncompressed +file is removed. diff --git a/compression_utils/xzize b/compression_utils/xzize new file mode 100755 index 0000000..625e537 --- /dev/null +++ b/compression_utils/xzize @@ -0,0 +1,102 @@ +#!/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. +# +# This script recompresses to xz (with maximum compression level) a +# GZip (.gz), BZip2 (.bz2), LZMA (.lzma), and Lempel-Ziv (.Z) +# compressed file. The original file is keeped. +# If the file extension does not correspond to a known compression +# format, the file is compressed to xz. In that case, the original +# file is removed. + + +set -u + + +test_extension() +{ + local - FILE EXTENSION BASE + FILE="$1" + EXTENSION="$2" + BASE="`basename "$FILE" ."$EXTENSION"`" + if [ "$BASE" != "$FILE" ] ; then + DEST="$BASE".xz + return 0 + fi + 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 »…" + $ACTION -c $SOURCE | xz -9 >$DEST + return $? +} + + +## Verify number of arguments ## + +if [ $# -lt 1 ] ; then + echo "$PROGRAM requires at least one argument!" >&2 + exit 2 +fi + + +## Process files ## + +for SOURCE in "$@" ; do + + # GZip + test_extension "$SOURCE" gz + if [ "$DEST" != "$SOURCE" ] ; then + ACTION=gunzip + do_recompress + continue + fi + + # BZip2 + test_extension "$SOURCE" bz2 + if [ "$DEST" != "$SOURCE" ] ; then + ACTION=bunzip2 + do_recompress + continue + fi + + # LZMA + test_extension "$SOURCE" lzma + if [ "$DEST" != "$SOURCE" ] ; then + ACTION=unlzma + do_recompress + continue + fi + + # Compress + test_extension "$SOURCE" Z + if [ "$DEST" != "$SOURCE" ] ; then + ACTION=uncompress + do_recompress + continue + fi + + # XZ + if test_extension "$SOURCE" xz ; then + echo "Skipping source file « $DEST »: it has already .xz suffix!" >&2 + continue + fi + + # Uncompressed file: fall back to simple compression + xz -9 "$SOURCE" + +done