zmore for bzip2?

Mikko Työläjärvi mbsd at pacbell.net
Sun Aug 31 12:50:12 PDT 2003


On Sun, 31 Aug 2003 pbdlists at pinboard.com wrote:

> Adapting zmore for the case where you specify the files to display on
> the command line is not problem at all:
>
>      > diff kk zmore
>      5,14d4
>      < get_decompressor ()
>      < {
>      <   case `file ${1--} | sed "s/[^:]*: *\([^ ]*\).*/\1/"` in
>      <     "compress"*) DECOMPRESSOR="uncompress -c";;
>      <     "gzip"*)     DECOMPRESSOR="gzip -cdfq";;
>      <     "bzip2"*)    DECOMPRESSOR="bunzip2 -cdq";;
>      <     *)           DECOMPRESSOR="cat";;
>      <   esac
>      < }
>      <
>      56,57c46
>      <                 get_decompressor ${FILE}
>      <               ${DECOMPRESSOR} "$FILE" | eval ${PAGER-more}
>      ---
>      >               gzip -cdfq "$FILE" | eval ${PAGER-more}
>
> But when zmore is used as a pipe or with input redirection, things become
> more complicated.
>
>                gzip -cdfq | eval ${PAGER-more}
>
> In order to detect the type of data passed on STDIN, the get_decompressor
> function or any other means of detection would consume STDIN. STDIN, however
> must be passed to the decompressor after the type of data has been
> detected. I don't have an idea hot to 'duplicate' STDIN, so it could be

As a somewhat ugly hack, you could read a few bytes, match against
known magic numbers and then prepend those bytes to the stream before
feeding to the decompressor, for the cost of an extra "cat" process:

   magic=$(dd bs=1 count=3 2>/dev/null)
   case $magic in
       BZh) DECOMPRESSOR=bunzip2;;
       *)   DECOMPRESSOR="gzip -cdfq";;
   esac
   (echo -n "$magic"; exec cat) | $DECOMPRESSOR | eval ${PAGER-more}

Note that gzip will handle compressed data and gzipped data as
well as plain text.

     $.02,
     /Mikko




> consumed twice. Sure, writing it to a temporary file would be a
> workaround:
>
>      38,53c28
>      <         FILE="/tmp/.zmore.${$}"
>      <         touch ${FILE} 2>/dev/null
>      <         if [ "${?}" -ne "0" ]; then
>      <           echo "can't create temporary file"
>      <           exit 1
>      <         fi
>      <         chmod 0600 ${FILE}
>      <         cat >${FILE}
>      <         if [ "${?}" -ne "0" ]; then
>      <           echo "can't create temporary file"
>      <           rm -f ${FILE}
>      <           exit 1
>      <         fi
>      <         get_decompressor ${FILE}
>      <         cat ${FILE} | ${DECOMPRESSOR} | eval ${PAGER-more}
>      <         rm -f ${FILE}
>      ---
>      >         gzip -cdfq | eval ${PAGER-more}
>
> But I don't like that. I myself sometimes work with compressed files
> larger than anything I would be happy to write to /tmp or somewhere else
> (even though those cases usually rather use zcat and its cousinds than
> zmore...)
>
> Kurt
>
> On Sat, Aug 30, 2003 at 12:21:45PM -0400, Chuck Swiger wrote:
> > David Kelly wrote:
> > [ ... ]
> > > Yes, of course. But zmore is smart enough to figure out what to do with
> > > several compression techniques, or even to handle non-compressed files
> > > very trivially and without hassle.
> >
> > 'zmore' is a simple shell script which calls "gzcat | ${PAGER-more}".  One
> > solution to your problem, or at least a solution, would be to change zmore to
> > look for a trailing bz/bz2 or invoke bzcat instead.  Another would be to change
> > the sources of gzip to recognize the bzip2 magic files bytes, extending the
> > detection of gzip versus classic LZH used by compress.
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
>


More information about the freebsd-questions mailing list