"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/extra/to-7zip.in" (28 Mar 2021, 6897 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "to-7zip.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # to-7zip: Converts .Z, .zip, .rar, .gz, .lzo, .bz2, and .zst files to 7-zip
    5 # format.
    6 
    7 # Copyright (C) 2006-2021 by Brian Lindholm.  This file is part of the
    8 # littleutils utility set.
    9 #
   10 # The to-7zip utility is free software; you can redistribute it and/or modify
   11 # it under the terms of the GNU General Public License as published by the Free
   12 # Software Foundation; either version 3, or (at your option) any later version.
   13 #
   14 # The to-7zip utility is distributed in the hope that it will be useful, but
   15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   16 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   17 # more details.
   18 #
   19 # You should have received a copy of the GNU General Public License along with
   20 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   21 
   22 # get command-line options
   23 DELORIG='n'
   24 MAXCOMP='n'
   25 OVERWRITE='y'
   26 PREFER=''
   27 RETAINTIME='n'
   28 while getopts 46dhkmnt opts
   29 do
   30   case $opts in
   31     4) PREFER='--prefer-family=IPv4' ;;
   32     6) PREFER='--prefer-family=IPv6' ;;
   33     d) DELORIG='y' ;;
   34     h) echo 'to-7zip LU_VERSION'
   35        echo 'usage: to-7zip [-(IPv)4] [-(IPV)6] [-d(elete_original)] [-k(eep_smallest)]'
   36        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-t(imestamp retain)]'
   37        echo '               [-h(elp)] filename ...'
   38        exit 0 ;;
   39     k) DELORIG='small' ;;
   40     m) MAXCOMP='y' ;;
   41     n) OVERWRITE='n' ;;
   42     t) RETAINTIME='y' ;;
   43     *) echo 'to-7zip LU_VERSION'
   44        echo 'usage: to-7zip [-(IPv)4] [-(IPV)6] [-d(elete_original)] [-k(eep_smallest)]'
   45        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-t(imestamp retain)]'
   46        echo '               [-h(elp)] filename ...'
   47        exit 1 ;;
   48   esac
   49 done
   50 shift $((${OPTIND}-1))
   51 
   52 # compute ancillary parameters
   53 if [ "$MAXCOMP" = 'y' ]; then
   54   OPTSLZMA='-mx=9'
   55 else
   56   OPTSLZMA='-mx=7'
   57 fi
   58 
   59 # set up traps
   60 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   61 
   62 # run through files
   63 declare -i S0=0 S1=0
   64 while [ $# -gt 0 ]; do
   65 
   66   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   67   NAME="$1"
   68   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   69     wget ${PREFER} -q "$NAME"
   70     NAME="${NAME##*/}"
   71     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   72       echo "$NAME failed to download!"
   73       shift; continue
   74     fi
   75   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   76     echo "$NAME is not a readable file!"
   77     shift; continue
   78   fi
   79   echo -n "${NAME}... "
   80 
   81   # determine filepath and check for writeability
   82   FILENAME="${NAME##*/}"
   83   FILEPATH="${NAME%${FILENAME}}"
   84   FILEPATH="${FILEPATH%/}"
   85   if [ "X$FILEPATH" = 'X' ]; then
   86     FILEPATH='.'
   87   fi
   88   if [ ! -w "$FILEPATH" ]; then
   89     echo 'failed because target directory is non-writeable'
   90     shift; continue
   91   fi
   92 
   93   # determine new name and check for availability
   94   if  [ "${NAME#${NAME%.bz2}}" = '.bz2' ]; then
   95     NEWNAME="${NAME%.bz2}.7z"
   96   elif [ "${NAME#${NAME%.tbz}}" = '.tbz' ]; then
   97     NEWNAME="${NAME%.tbz}.t7z"
   98   elif  [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
   99     NEWNAME="${NAME%.gz}.7z"
  100   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
  101     NEWNAME="${NAME%.tgz}.t7z"
  102   elif  [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  103     NEWNAME="${NAME%.lzo}.7z"
  104   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  105     NEWNAME="${NAME%.rar}.tar.7z"
  106   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  107     NEWNAME="${NAME%.RAR}.tar.7z"
  108   elif  [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  109     NEWNAME="${NAME%.Z}.7z"
  110   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  111     NEWNAME="${NAME%.zip}.tar.7z"
  112   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  113     NEWNAME="${NAME%.ZIP}.tar.7z"
  114   elif  [ "${NAME#${NAME%.zst}}" = '.zst' ]; then
  115     NEWNAME="${NAME%.zst}.7z"
  116   else
  117     NEWNAME="${NAME}.7z"
  118   fi
  119   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  120     echo 'skipped because target already exists'
  121     shift; continue
  122   fi
  123 
  124   # recompress based on filetype
  125   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  126   # create temporary target name (note that this is currently insecure,
  127   # as p7zip annoyingly refuses to overwrite a zero-length file)
  128   TMPFILE=$(tempname -c -d "$FILEPATH" -s .7z .to-7zip_$$) || exit 99
  129   if [ "X$FILETYPE" = 'X7-zi' ]; then
  130     echo 'already converted to 7zip'
  131     rm -f $TMPFILE
  132     shift; continue
  133   elif [ "X$FILETYPE" = 'Xbzip' ]; then
  134     bzip2 -c -d -q "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  135     if [ $? -ne 0 ]; then
  136       echo 'failed on bzip2-to-7zip conversion'
  137       rm -f $TMPFILE
  138       shift; continue
  139     fi
  140   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  141     gzip -c -d -q "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  142     if [ $? -ne 0 ]; then
  143       echo 'failed on gzip-to-7zip conversion'
  144       rm -f $TMPFILE
  145       shift; continue
  146     fi
  147   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  148     lzop -c -d -q "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  149     if [ $? -ne 0 ]; then
  150       echo 'failed on lzop-to-7zip conversion'
  151       rm -f $TMPFILE
  152       shift; continue
  153     fi
  154   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  155     gzip -c -d -q "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  156     if [ $? -ne 0 ]; then
  157       echo 'failed on compress-to-7zip conversion'
  158       rm -f $TMPFILE
  159       shift; continue
  160     fi
  161   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  162     rar2tarcat "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  163     if [ $? -ne 0 ]; then
  164       echo 'failed on rar-to-7zip conversion'
  165       rm -f $TMPFILE
  166       shift; continue
  167     fi
  168   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  169     zip2tarcat "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  170     if [ $? -ne 0 ]; then
  171       echo 'failed on zip-to-7zip conversion'
  172       rm -f $TMPFILE
  173       shift; continue
  174     fi
  175   elif [ "X$FILETYPE" = 'Xzsta' ]; then
  176     zstd -c -d -q "$NAME" | PROG7Z a -si -bd $OPTSLZMA $TMPFILE > /dev/null
  177     if [ $? -ne 0 ]; then
  178       echo 'failed on zst-to-7zip conversion'
  179       rm -f $TMPFILE
  180       shift; continue
  181     fi
  182   else
  183     echo 'skipped on unrecognized filetype'
  184     rm -f $TMPFILE
  185     shift; continue
  186   fi
  187 
  188   # perform the final move, deleting old or new if necessary
  189   chmod --reference="$NAME" $TMPFILE
  190   mv $TMPFILE "$NEWNAME"
  191   if [ "$RETAINTIME" = 'y' ]; then
  192     touch --reference="$NAME" "$NEWNAME"
  193   fi
  194   S0=$(filesize "$NAME")
  195   S1=$(filesize "$NEWNAME")
  196   if [ $S1 -lt $S0 ]; then
  197     if [ "$DELORIG" != 'n' ]; then
  198       rm -f "$NAME"
  199       echo "$S0 vs. $S1 (deleting original)"
  200     else
  201       echo "$S0 vs. $S1"
  202     fi
  203   else
  204     if [ "$DELORIG" = 'n' ]; then
  205       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  206     elif [ "$DELORIG" = 'y' ]; then
  207       rm -f "$NAME"
  208       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  209     else
  210       rm -f "$NEWNAME"
  211       echo 'new file deleted (not smaller than old)'
  212     fi
  213   fi
  214 
  215   shift
  216 done