codekingpro/portable-devtools
114k
1#!/bin/sh2 3# Copyright (C) 1998, 2002, 2004, 2007, 2010-2025 Free Software Foundation,4# Inc.5# Copyright (C) 1993 Jean-loup Gailly6 7# This program is free software; you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation; either version 3 of the License, or10# (at your option) any later version.11 12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16 17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <https://www.gnu.org/licenses/>.19 20LC_ALL=C21export LC_ALL22 23version="znew (gzip) 1.1424Copyright (C) 2025 Free Software Foundation, Inc.25This is free software. You may redistribute copies of it under the terms of26the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.27There is NO WARRANTY, to the extent permitted by law.28 29Written by Jean-loup Gailly."30 31usage="Usage: $0 [OPTION]... [FILE]...32Recompress files from .Z (compress) format to .gz (gzip) format.33 34Options:35 36 -f Force recompression even if a .gz file already exists.37 -t Test the new files before deleting originals.38 -v Verbose; display name and statistics for each file compressed.39 -9 Use the slowest compression method (optimal compression).40 -P Use pipes for the conversion to reduce disk space usage.41 -K Keep a .Z file when it is smaller than the .gz file; implies -t.42 --help display this help and exit43 --version output version information and exit44 45Report bugs to <bug-gzip@gnu.org>."46 47check=048pipe=049opt=50files=51keep=052res=053old=054new=055block=102456# block is the disk block size (best guess, need not be exact)57 58# Beware -s or --suffix in $GZIP, which could cause misbehavior59# in gzip 1.13 and earlier.60unset GZIP61ext=.gz62 63for arg64do65 case "$arg" in66 --help) printf '%s\n' "$usage" || exit 1; exit;;67 --version) printf '%s\n' "$version" || exit 1; exit;;68 -*) opt="$opt $arg"; shift;;69 *) break;;70 esac71done72 73if test $# -eq 0; then74 echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"75 exit 176fi77 78opt=`printf '%s\n' "$opt" | sed -e 's/ //g' -e 's/-//g'`79case "$opt" in80 *t*) check=1; opt=`printf '%s\n' "$opt" | sed 's/t//g'`81esac82case "$opt" in83 *K*) keep=1; check=1; opt=`printf '%s\n' "$opt" | sed 's/K//g'`84esac85case "$opt" in86 *P*) pipe=1; opt=`printf '%s\n' "$opt" | sed 's/P//g'`87esac88if test -n "$opt"; then89 opt="-$opt"90fi91 92for i do93 n=`printf '%s\n' "$i" | sed 's/.Z$//'`94 if test ! -f "$n.Z" ; then95 printf '%s\n' "$n.Z not found"96 res=1; continue97 fi98 test $keep -eq 1 && old=`wc -c < "$n.Z"`99 if test $pipe -eq 1; then100 if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then101 # Copy file attributes from old file to new one, if possible.102 touch -r"$n.Z" -- "$n$ext" 2>/dev/null103 chmod --reference="$n.Z" -- "$n$ext" 2>/dev/null104 else105 printf '%s\n' "error while recompressing $n.Z"106 res=1; continue107 fi108 else109 if test $check -eq 1; then110 if cp -p "$n.Z" "$n.$$"; then111 :112 else113 printf '%s\n' "cannot backup $n.Z"114 res=1; continue115 fi116 fi117 if gzip -d "$n.Z"; then118 :119 else120 test $check -eq 1 && mv "$n.$$" "$n.Z"121 printf '%s\n' "error while uncompressing $n.Z"122 res=1; continue123 fi124 if gzip $opt "$n"; then125 :126 else127 if test $check -eq 1; then128 mv "$n.$$" "$n.Z" && rm -f "$n"129 printf '%s\n' "error while recompressing $n"130 else131 # compress $n (might be dangerous if disk full)132 printf '%s\n' "error while recompressing $n, left uncompressed"133 fi134 res=1; continue135 fi136 fi137 test $keep -eq 1 && new=`wc -c < "$n$ext"`138 if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \139 `expr \( $new + $block - 1 \) / $block`; then140 if test $pipe -eq 1; then141 rm -f "$n$ext"142 else143 mv "$n.$$" "$n.Z" && rm -f "$n$ext"144 fi145 printf '%s\n' "$n.Z smaller than $n$ext -- unchanged"146 147 elif test $check -eq 1; then148 if gzip -t "$n$ext" ; then149 rm -f "$n.$$" "$n.Z"150 else151 test $pipe -eq 0 && mv "$n.$$" "$n.Z"152 rm -f "$n$ext"153 printf '%s\n' "error while testing $n$ext, $n.Z unchanged"154 res=1; continue155 fi156 elif test $pipe -eq 1; then157 rm -f "$n.Z"158 fi159done160exit $res161 