home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Edition 1: Linux / CD1.iso / doc / HOWTO / mini / Bzip2 < prev    next >
Text File  |  1998-10-14  |  21KB  |  859 lines

  1.   Bzip2 Howto
  2.   David Fetter, dfetter@best.com <mailto:dfetter@best.com>
  3.   v1.92, 18 August 1998
  4.  
  5.   This document tells how to use the new bzip2 compression program.  The
  6.   local copy of the sgml at the current site is here <Bzip2-HOWTO.sgml>,
  7.   and the "author-itative" sgml is here <http://www.best.com/~dfet¡
  8.   ter/Bzip2-HOWTO/Bzip2-HOWTO.sgml>.
  9.   ______________________________________________________________________
  10.  
  11.   Table of Contents
  12.  
  13.  
  14.   1. Introduction
  15.  
  16.      1.1 (BF
  17.         1.1.1 v1.92 Updated the
  18.         1.1.2 v1.91
  19.         1.1.3 v1.9
  20.         1.1.4 v1.8
  21.         1.1.5 v1.7
  22.         1.1.6 v1.6
  23.         1.1.7 v1.5
  24.         1.1.8 v1.4
  25.         1.1.9 v1.3
  26.         1.1.10 v1.2
  27.         1.1.11 v1.1
  28.         1.1.12 v1.0
  29.  
  30.   2. Getting bzip2
  31.  
  32.      2.1 Bzip2-HOWTO in your language
  33.      2.2 Getting bzip2 precompiled binaries
  34.      2.3 Getting bzip2 sources
  35.      2.4 Compiling bzip2 for your machine
  36.  
  37.   3. Using bzip2 by itself
  38.  
  39.   4. Using bzip2 with tar
  40.  
  41.      4.1 Easiest to set up:
  42.      4.2 Easy to set up, fairly easy to use, no need for root privileges:
  43.      4.3 Also easy to use, but needs root access.
  44.  
  45.   5. Using bzip2 with less
  46.  
  47.   6. Using bzip2 with emacs
  48.  
  49.      6.1 Changing emacs for everyone:
  50.      6.2 Changing emacs for one person:
  51.  
  52.   7. Using bzip2 with wu-ftpd
  53.  
  54.   8. Using bzip2 with grep
  55.  
  56.   9. Using bzip2 with Netscape under the X.
  57.  
  58.   10. Using bzip2 to recompress other compression formats
  59.  
  60.  
  61.  
  62.   ______________________________________________________________________
  63.  
  64.  
  65.  
  66.  
  67.   1.  Introduction
  68.  
  69.   Bzip2 is a groovy new algorithm for compressing data.  It generally
  70.   makes files that are 60-70% of the size of their gzip'd counterparts.
  71.  
  72.  
  73.   This document will take you through a few common applications for
  74.   bzip2.
  75.  
  76.  
  77.   Future versions of the document will discuss the upcoming bzip2
  78.   library which bzip2's author, Julian Seward
  79.   <mailto:Julian_Seward@muraroa.demon.co.uk> describes as follows:
  80.  
  81.  
  82.   I'm working on the next version of bzip2, which will use the same
  83.   .bz2 file format; the main addition is a zlib-like library for
  84.   reading and writing data in that format from within programs.
  85.  
  86.  
  87.  
  88.  
  89.   Future versions of the document may also include a summary of the
  90.   discussion over whether (and how) bzip2 should be used in the Linux
  91.   kernel.
  92.  
  93.  
  94.   1.1.  Revision History
  95.  
  96.   1.1.1.  Updated the``Getting bzip2 binaries'' section, including
  97.   adding S.u.S.E.'s.  v1.92
  98.  
  99.   1.1.2.  v1.91
  100.  
  101.   Corrected a typo and clarified some shell idioms in the ``section on
  102.   using bzip2 with tar''.  Thanks to Alessandro Rubini for these.
  103.  
  104.  
  105.   Updated the buzzit tool not to stomp on the original bzip2 archive.
  106.  
  107.  
  108.   Added bgrep, a zgrep-like tool.
  109.  
  110.  
  111.   1.1.3.  v1.9
  112.  
  113.   Clarified the gcc 2.7.* problem.  Thanks to Ulrik Dickow for pointing
  114.   this out.
  115.  
  116.  
  117.   Added Leonard Jean-Marc's elegant way to work with tar.
  118.  
  119.  
  120.   Added Linus ├kerlund's Swedish translation.
  121.  
  122.  
  123.   Fixed the wu-ftpd section per Arnaud Launay's suggestion.
  124.  
  125.  
  126.   Moved translations to their own section.
  127.  
  128.  
  129.   1.1.4.  v1.8
  130.  
  131.   Put buzzit and tar.diff in the sgml where they belong. Fixed
  132.   punctuation and formatting.  Thanks to Arnaud Launay for his help
  133.   correcting my copy. :-)
  134.  
  135.  
  136.   Dropped xv project for now due to lack of popular interest.
  137.  
  138.  
  139.   Added teasers for future versions of the document.
  140.  
  141.  
  142.   1.1.5.  v1.7
  143.  
  144.   Added buzzit utility.  Fixed the patch against gnu tar.
  145.  
  146.  
  147.   1.1.6.  v1.6
  148.  
  149.   Added TenThumbs' Netscape enabler.
  150.  
  151.  
  152.   Also changed lesspipe.sh per his sugestion.  It should work better
  153.   now.
  154.  
  155.  
  156.   1.1.7.  v1.5
  157.  
  158.   Added Arnaud Launay's French translation, and his wu-ftpd file.
  159.  
  160.  
  161.   1.1.8.  v1.4
  162.  
  163.   Added Tetsu Isaji's Japanese translation.
  164.  
  165.  
  166.   1.1.9.  v1.3
  167.  
  168.   Added Ulrik Dickow's .emacs for 19.30 and higher.
  169.  
  170.  
  171.   (Also corrected jka-compr.el patch for emacs per his suggestion. Oops!
  172.   Bzip2's doesn't yet(?) have an "append" flag.)
  173.  
  174.  
  175.   1.1.10.  v1.2
  176.  
  177.   Changed patch for emacs so it automagically recognizes .bz2 files.
  178.  
  179.  
  180.   1.1.11.  v1.1
  181.  
  182.   Added patch for emacs.
  183.  
  184.  
  185.   1.1.12.  v1.0
  186.  
  187.   Round 1.
  188.  
  189.  
  190.  
  191.   2.  Getting bzip2
  192.  
  193.   Bzip2's home page is at The UK home site
  194.   <http://www.muraroa.demon.co.uk/>.  The United States mirror site is
  195.   here <http://www.digistar.com/bzip2/index.html>.  You can also find it
  196.   on Red Hat's ftp site here <ftp://ftp.redhat.com/pub/contrib>.
  197.  
  198.  
  199.   2.1.  Bzip2-HOWTO in your language
  200.  
  201.   French speakers may wish to refer to Arnaud Launay's French documents.
  202.   The web version is here
  203.   <http://www.freenix.fr/linux/HOWTO/mini/Bzip2.html>, and you can use
  204.   ftp here <ftp://ftp.lip6.fr/pub/linux/french/docs/HOWTO/mini/Bzip2.gz>
  205.   Arnaud can be contacted by electronic mail at this address
  206.   <mailto:zoro@mygale.org>
  207.  
  208.  
  209.   Japanese speakers may wish to refer to Tetsu Isaji's Japanese
  210.   documents here <http://jf.gee.kyoto-u.ac.jp/JF/JF.html>.  Isaji can be
  211.   reached at his home page <http://www2s.biglobe.ne.jp/~kaien/>, or by
  212.   electronic mail at this address. <mailto:isaji@mxu.meshnet.or.jp>
  213.  
  214.  
  215.   Swedish speakers may wish to refer to Linus ├kerlund's Swedish
  216.   documents here <http://user.tninet.se/~uxm165t/linux_doc.html>.  Linus
  217.   can be reached by electronic mail at this address.
  218.   <mailto:uxm165t@tninet.se>
  219.  
  220.  
  221.   2.2.  Getting bzip2 precompiled binaries
  222.  
  223.   See the home sites.
  224.  
  225.  
  226.   Debian's Intel binary is
  227.   <ftp://ftp.debian.org/debian/dists/stable/main/binary-
  228.   i386/utils/bzip2_0.1pl2-5.deb name=>>.
  229.  
  230.  
  231.   Red Hat's alpha binary is here
  232.   <ftp://ftp.redhat.com/pub/redhat/redhat-5.1/alpha/RedHat/RPMS/bzip2-0.1pl2-1.alpha.rpm>.
  233.  
  234.  
  235.   Red Hat's Intel binary is here
  236.   <ftp://ftp.redhat.com/pub/redhat/redhat-5.1/i386/RedHat/RPMS/bzip2-0.1pl2-1.i386.rpm>.
  237.  
  238.  
  239.   Red Hat's SPARC binary is here
  240.   <ftp://ftp.redhat.com/pub/redhat/redhat-5.1/sparc/RedHat/RPMS/bzip2-0.1pl2-1.sparc.rpm>.
  241.  
  242.  
  243.   Slackware's Intel binary is here
  244.   <ftp://www.cdrom.com/pub/linux/slackware-3.5/slakware/a1/bzip2.tgz>.
  245.  
  246.  
  247.   S.u.S.E.'s Intel binary is here <ftp://ftp.suse.com/pub/SuSE-
  248.   Linux/5.2/suse/ap1/bzip.rpm>.
  249.  
  250.   You can also get these in the analogous places at the various mirror
  251.   sites.
  252.  
  253.  
  254.   2.3.  Getting bzip2 sources
  255.  
  256.   They come from the Official sites (see ``Getting Bzip2'' for where, or
  257.   Red Hat has it here
  258.   <ftp://ftp.redhat.com/pub/contrib/SRPMS/bzip2-0.1pl2-1.src.rpm>).
  259.  
  260.  
  261.   2.4.  Compiling bzip2 for your machine
  262.  
  263.   If you have gcc 2.7.*, change the line that reads
  264.  
  265.   CFLAGS = -O3 -fomit-frame-pointer -funroll-loops
  266.  
  267.  
  268.  
  269.  
  270.   to
  271.  
  272.  
  273.        CFLAGS = -O2 -fomit-frame-pointer
  274.  
  275.  
  276.  
  277.  
  278.   that is, replace -O3 with -O2 and drop the -funroll-loops.  You may
  279.   also wish to add  any -m* flags (like -m486, for example) you use when
  280.   compiling kernels.
  281.  
  282.   Avoiding -funroll-loops is the most important part, since this will
  283.   cause many gcc 2.7's to generate wrong code, and all gcc 2.7's to
  284.   generate slower and larger code.  For other compilers (lcc, egcs, gcc
  285.   2.8.x) the default CFLAGS are fine.
  286.  
  287.   After that, just make it and install it per the README.
  288.  
  289.  
  290.   3.  Using bzip2 by itself
  291.  
  292.   Read the Fine Manual Page :)
  293.  
  294.  
  295.   4.  Using bzip2 with tar
  296.  
  297.   Listed below are three ways to use bzip2 with tar, namely
  298.  
  299.   4.1.  Easiest to set up:
  300.  
  301.   This method requires no setup at all.  To un-tar the bzip2'd tar
  302.   archive, foo.tar.bz2 in the current directory, do
  303.  
  304.  
  305.        /path/to/bzip2 -cd foo.tar.bz2 | tar xf -
  306.  
  307.  
  308.  
  309.  
  310.   This works, but can be a PITA to type often.
  311.  
  312.  
  313.   4.2.  Easy to set up, fairly easy to use, no need for root privileges:
  314.  
  315.   Thanks to Leonard Jean-Marc <mailto:leonard@sct1.is.belgacom.be> for
  316.   the tip.  Thanks also to Alessandro Rubini
  317.   <mailto:rubini@morgana.systemy.it> for differentiating bash from the
  318.   csh's.
  319.  
  320.  
  321.   In your .bashrc, you can put in a line like this:
  322.  
  323.  
  324.        alias btar='tar --use-compress-program /usr/local/bin/bzip2 '
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.   In your .tcshrc, or .cshrc, the analogous line looks like this:
  332.  
  333.  
  334.        alias btar 'tar --use-compress-program /usr/local/bin/bzip2'
  335.  
  336.  
  337.  
  338.  
  339.  
  340.   4.3.  Also easy to use, but needs root access.
  341.  
  342.   Apply the patch below to gnu tar 1.12 as follows:
  343.  
  344.  
  345.        cd tar-1.12/src; patch < /path/to/tar.diff
  346.  
  347.  
  348.  
  349.  
  350.   compile it, and install it, and you're good to go.  Make sure that
  351.   both tar and bzip2 are in your $PATH by "which tar" and "which bzip2."
  352.   To use the new tar, just do
  353.  
  354.  
  355.         tar xyf foo.tar.bz2
  356.  
  357.  
  358.  
  359.  
  360.   to decompress the file.
  361.  
  362.   To make a new archive, it's similar:
  363.  
  364.  
  365.         tar cyf foo.tar.bz2 file1 file2 file3...directory1 directory2...
  366.  
  367.  
  368.  
  369.  
  370.   Here is the patch:
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.   *** tar.c       Thu Jun 11 00:09:23 1998
  398.   --- tar.c.new   Thu Jun 11 00:14:24 1998
  399.   ***************
  400.   *** 196,201 ****
  401.   --- 196,203 ----
  402.       {"block-number", no_argument, NULL, 'R'},
  403.       {"block-size", required_argument, NULL, OBSOLETE_BLOCKING_FACTOR},
  404.       {"blocking-factor", required_argument, NULL, 'b'},
  405.   +   {"bzip2", required_argument, NULL, 'y'},
  406.   +   {"bunzip2", required_argument, NULL, 'y'},
  407.       {"catenate", no_argument, NULL, 'A'},
  408.       {"checkpoint", no_argument, _option, 1},
  409.       {"compare", no_argument, NULL, 'd'},
  410.   ***************
  411.   *** 372,377 ****
  412.   --- 374,380 ----
  413.                   PATTERN                at list/extract time, a globbing PATTERN\n\
  414.       -o, --old-archive, --portability   write a V7 format archive\n\
  415.           --posix                        write a POSIX conformant archive\n\
  416.   +   -y, --bzip2, --bunzip2             filter the archive through bzip2\n\
  417.       -z, --gzip, --ungzip               filter the archive through gzip\n\
  418.       -Z, --compress, --uncompress       filter the archive through compress\n\
  419.           --use-compress-program=PROG    filter through PROG (must accept -d)\n"),
  420.   ***************
  421.   *** 448,454 ****
  422.        Y  per-block gzip compression */
  423.  
  424.     #define OPTION_STRING \
  425.   !   "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"
  426.  
  427.     static void
  428.     set_subcommand_option (enum subcommand subcommand)
  429.   --- 451,457 ----
  430.        Y  per-block gzip compression */
  431.  
  432.     #define OPTION_STRING \
  433.   !   "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxyz"
  434.  
  435.     static void
  436.     set_subcommand_option (enum subcommand subcommand)
  437.   ***************
  438.   *** 805,810 ****
  439.   --- 808,817 ----
  440.           case 'X':
  441.           exclude_option = 1;
  442.           add_exclude_file (optarg);
  443.   +       break;
  444.   +
  445.   +       case 'y':
  446.   +       set_use_compress_program_option ("bzip2");
  447.           break;
  448.  
  449.           case 'z':
  450.  
  451.  
  452.  
  453.  
  454.   5.  Using bzip2 with less
  455.  
  456.   To uncompress bzip2'd files on the fly, i.e. to be able to use "less"
  457.   on them without first bunzip2'ing them, you can make a lesspipe.sh
  458.   (man less) like this:
  459.  
  460.  
  461.  
  462.  
  463.   #!/bin/sh
  464.   # This is a preprocessor for 'less'.  It is used when this environment
  465.   # variable is set:   LESSOPEN="|lesspipe.sh %s"
  466.  
  467.     case "$1" in
  468.     *.tar) tar tvvf $1 2>/dev/null ;; # View contents of various tar'd files
  469.     *.tgz) tar tzvvf $1 2>/dev/null ;;
  470.   # This one work for the unmodified version of tar:
  471.     *.tar.bz2) bzip2 -cd $1 $1 2>/dev/null | tar tzvvf - ;;
  472.   #This one works with the patched version of tar:
  473.   # *.tar.bz2) tyvvf $1 2>/dev/null ;;
  474.     *.tar.gz) tar tzvvf $1 2>/dev/null ;;
  475.     *.tar.Z) tar tzvvf $1 2>/dev/null ;;
  476.     *.tar.z) tar tzvvf $1 2>/dev/null ;;
  477.     *.bz2) bzip2 -dc $1  2>/dev/null ;; # View compressed files correctly
  478.     *.Z) gzip -dc $1  2>/dev/null ;;
  479.     *.z) gzip -dc $1  2>/dev/null ;;
  480.     *.gz) gzip -dc $1  2>/dev/null ;;
  481.     *.zip) unzip -l $1 2>/dev/null ;;
  482.     *.1|*.2|*.3|*.4|*.5|*.6|*.7|*.8|*.9|*.n|*.man) FILE=`file -L $1` ; # groff src
  483.       FILE=`echo $FILE | cut -d ' ' -f 2`
  484.       if [ "$FILE" = "troff" ]; then
  485.         groff -s -p -t -e -Tascii -mandoc $1
  486.       fi ;;
  487.     *) cat $1 2>/dev/null ;;
  488.   #  *) FILE=`file -L $1` ; # Check to see if binary, if so -- view with 'strings'
  489.   #    FILE1=`echo $FILE | cut -d ' ' -f 2`
  490.   #    FILE2=`echo $FILE | cut -d ' ' -f 3`
  491.   #    if [ "$FILE1" = "Linux/i386" -o "$FILE2" = "Linux/i386" \
  492.   #         -o "$FILE1" = "ELF" -o "$FILE2" = "ELF" ]; then
  493.   #      strings $1
  494.   #    fi ;;
  495.     esac
  496.  
  497.  
  498.  
  499.  
  500.   6.  Using bzip2 with emacs
  501.  
  502.   6.1.  Changing emacs for everyone:
  503.  
  504.   I've written the following patch to jka-compr.el which adds bzip2 to
  505.   auto-compression-mode.
  506.  
  507.   Disclaimer: I have only tested this with emacs-20.2, but have no
  508.   reason to believe that a similar approach won't work with other
  509.   versions.
  510.  
  511.   To use it,
  512.  
  513.   1. Go to the emacs-20.2/lisp source directory (wherever you untarred
  514.      it)
  515.  
  516.   2. Put the patch below in a file called jka-compr.el.diff (it should
  517.      be alone in that file ;).
  518.  
  519.   3. Do
  520.  
  521.  
  522.         patch < jka-compr.el.diff
  523.  
  524.  
  525.  
  526.  
  527.   4. Start emacs, and do
  528.  
  529.    M-x byte-compile-file jka-compr.el
  530.  
  531.  
  532.  
  533.  
  534.   5. Leave emacs.
  535.  
  536.   6. Move your original jka-compr.elc to a safe place in case of bugs.
  537.  
  538.   7. Replace it with the new jka-compr.elc.
  539.  
  540.   8. Have fun!
  541.  
  542.  
  543.      --- jka-compr.el        Sat Jul 26 17:02:39 1997
  544.      +++ jka-compr.el.new    Thu Feb  5 17:44:35 1998
  545.      @@ -44,7 +44,7 @@
  546.       ;; The variable, jka-compr-compression-info-list can be used to
  547.       ;; customize jka-compr to work with other compression programs.
  548.       ;; The default value of this variable allows jka-compr to work with
  549.      -;; Unix compress and gzip.
  550.      +;; Unix compress and gzip.  David Fetter added bzip2 support :)
  551.       ;;
  552.       ;; If you are concerned about the stderr output of gzip and other
  553.       ;; compression/decompression programs showing up in your buffers, you
  554.      @@ -121,7 +121,9 @@
  555.  
  556.  
  557.       ;;; I have this defined so that .Z files are assumed to be in unix
  558.      -;;; compress format; and .gz files, in gzip format.
  559.      +;;; compress format; and .gz files, in gzip format, and .bz2 files,
  560.      +;;; in the snappy new bzip2 format from http://www.muraroa.demon.co.uk.
  561.      +;;; Keep up the good work, people!
  562.       (defcustom jka-compr-compression-info-list
  563.         ;;[regexp
  564.         ;; compr-message  compr-prog  compr-args
  565.      @@ -131,6 +133,10 @@
  566.            "compressing"    "compress"     ("-c")
  567.            "uncompressing"  "uncompress"   ("-c")
  568.            nil t]
  569.      +    ["\\.bz2\\'"
  570.      +     "bzip2ing"        "bzip2"         ("")
  571.      +     "bunzip2ing"      "bzip2"         ("-d")
  572.      +     nil t]
  573.           ["\\.tgz\\'"
  574.            "zipping"        "gzip"         ("-c" "-q")
  575.            "unzipping"      "gzip"         ("-c" "-q" "-d")
  576.  
  577.  
  578.  
  579.  
  580.   6.2.  Changing emacs for one person:
  581.  
  582.   Thanks for this one go to Ulrik Dickow, ukd@kampsax.dk
  583.   <mailto:ukdATkampsax.dk>, Systems Programmer at Kampsax Technology:
  584.  
  585.   To make it so you can use bzip2 automatically when you aren't the
  586.   sysadmin, just add the following to your .emacs file.
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.   ;; Automatic (un)compression on loading/saving files (gzip(1) and similar)
  596.   ;; We start it in the off state, so that bzip2(1) support can be added.
  597.   ;; Code thrown together by Ulrik Dickow for ~/.emacs with Emacs 19.34.
  598.   ;; Should work with many older and newer Emacsen too.  No warranty though.
  599.   ;;
  600.   (if (fboundp 'auto-compression-mode) ; Emacs 19.30+
  601.       (auto-compression-mode 0)
  602.     (require 'jka-compr)
  603.     (toggle-auto-compression 0))
  604.   ;; Now add bzip2 support and turn auto compression back on.
  605.   (add-to-list 'jka-compr-compression-info-list
  606.                ["\\.bz2\\(~\\|\\.~[0-9]+~\\)?\\'"
  607.                 "zipping"        "bzip2"         ()
  608.                 "unzipping"      "bzip2"         ("-d")
  609.                 nil t])
  610.   (toggle-auto-compression 1 t)
  611.  
  612.  
  613.  
  614.  
  615.   7.  Using bzip2 with wu-ftpd
  616.  
  617.   Thanks to Arnaud Launay for this bandwidth saver.  The following
  618.   should go in /etc/ftpconversions to do on-the-fly compressions and
  619.   decompressions with bzip2. Make sure that the paths (like
  620.   /bin/compress) are right.
  621.  
  622.  
  623.    :.Z:  :  :/bin/compress -d -c %s:T_REG|T_ASCII:O_UNCOMPRESS:UNCOMPRESS
  624.    :   : :.Z:/bin/compress -c %s:T_REG:O_COMPRESS:COMPRESS
  625.    :.gz: :  :/bin/gzip -cd %s:T_REG|T_ASCII:O_UNCOMPRESS:GUNZIP
  626.    :   : :.gz:/bin/gzip -9 -c %s:T_REG:O_COMPRESS:GZIP
  627.    :.bz2: :  :/bin/bzip2 -cd %s:T_REG|T_ASCII:O_UNCOMPRESS:BUNZIP2
  628.    :   : :.bz2:/bin/bzip2 -9 -c %s:T_REG:O_COMPRESS:BZIP2
  629.    :   : :.tar:/bin/tar -c -f - %s:T_REG|T_DIR:O_TAR:TAR
  630.    :   : :.tar.Z:/bin/tar -c -Z -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+COMPRESS
  631.    :   : :.tar.gz:/bin/tar -c -z -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+GZIP
  632.    :   : :.tar.bz2:/bin/tar -c -y -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+BZIP2
  633.  
  634.  
  635.  
  636.  
  637.   8.  Using bzip2 with grep
  638.  
  639.   The following utility, which I call bgrep, is a slight modification of
  640.   the zgrep which comes with Linux.  You can use it to grep through
  641.   files without bunzip2'ing them first.
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.   #!/bin/sh
  662.  
  663.   # bgrep -- a wrapper around a grep program that decompresses files as needed
  664.   PATH="/usr/bin:$PATH"; export PATH
  665.  
  666.   prog=`echo $0 | sed 's|.*/||'`
  667.   case "$prog" in
  668.           *egrep) grep=${EGREP-egrep}     ;;
  669.           *fgrep) grep=${FGREP-fgrep}     ;;
  670.           *)      grep=${GREP-grep}       ;;
  671.   esac
  672.   pat=""
  673.   while test $# -ne 0; do
  674.     case "$1" in
  675.     -e | -f) opt="$opt $1"; shift; pat="$1"
  676.              if test "$grep" = grep; then  # grep is buggy with -e on SVR4
  677.                grep=egrep
  678.              fi;;
  679.     -*)      opt="$opt $1";;
  680.      *)      if test -z "$pat"; then
  681.                pat="$1"
  682.              else
  683.                break;
  684.              fi;;
  685.     esac
  686.     shift
  687.   done
  688.  
  689.   if test -z "$pat"; then
  690.     echo "grep through bzip2 files"
  691.     echo "usage: $prog [grep_options] pattern [files]"
  692.     exit 1
  693.   fi
  694.  
  695.   list=0
  696.   silent=0
  697.   op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
  698.   case "$op" in
  699.     *l*) list=1
  700.   esac
  701.   case "$op" in
  702.     *h*) silent=1
  703.   esac
  704.  
  705.   if test $# -eq 0; then
  706.     bzip2 -cd | $grep $opt "$pat"
  707.     exit $?
  708.   fi
  709.  
  710.   res=0
  711.   for i do
  712.     if test $list -eq 1; then
  713.       bzip2 -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
  714.       r=$?
  715.     elif test $# -eq 1 -o $silent -eq 1; then
  716.       bzip2 -cd "$i" | $grep $opt "$pat"
  717.       r=$?
  718.     else
  719.       bzip2 -cd "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
  720.       r=$?
  721.     fi
  722.     test "$r" -ne 0 && res="$r"
  723.   done
  724.   exit $res
  725.  
  726.  
  727.   9.  Using bzip2 with Netscape under the X.
  728.  
  729.   tenthumbs@cybernex.net says:
  730.  
  731.  
  732.        I also found a way to get Linux Netscape to use bzip2 for Content-
  733.        Encoding just as it uses gzip. Add this to $HOME/.Xdefaults or
  734.        $HOME/.Xresources
  735.  
  736.        I use the -s option because I would rather trade some decompressing
  737.        speed for RAM usage. You can leave the option out if you want to.
  738.  
  739.  
  740.  
  741.   Netscape*encodingFilters:      \
  742.           x-compress :  : .Z     : uncompress -c  \n\
  743.           compress   :  : .Z     : uncompress -c  \n\
  744.           x-gzip     :  : .z,.gz : gzip -cdq      \n\
  745.           gzip       :  : .z,.gz : gzip -cdq      \n\
  746.           x-bzip2    :  : .bz2   : bzip2 -ds \n
  747.  
  748.  
  749.  
  750.  
  751.   10.  Using bzip2 to recompress other compression formats
  752.  
  753.   The following perl program takes files compressed in other formats
  754.   (.tar.gz, .tgz. .tar.Z, and .Z for this iteration) and repacks them
  755.   for better compression.  The perl source has all kinds of neat
  756.   documentation on what it does and how it does what it does.
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.   #!/usr/bin/perl -w
  794.  
  795.   #######################################################
  796.   #                                                     #
  797.   # This program takes compressed and gzipped programs  #
  798.   # in the current directory and turns them into bzip2  #
  799.   # format.  It handles the .tgz extension in a         #
  800.   # reasonable way, producing a .tar.bz2 file.          #
  801.   #                                                     #
  802.   #######################################################
  803.   $counter = 0;
  804.   $saved_bytes = 0;
  805.   $totals_file = '/tmp/machine_bzip2_total';
  806.   $machine_bzip2_total = 0;
  807.  
  808.   while(<*[Zz]>) {
  809.       next if /^bzip2-0.1pl2.tar.gz$/;
  810.       push @files, $_;
  811.   }
  812.   $total = scalar(@files);
  813.  
  814.   foreach (@files) {
  815.       if (/tgz$/) {
  816.           ($new=$_) =~ s/tgz$/tar.bz2/;
  817.       } else {
  818.           ($new=$_) =~ s/\.g?z$/.bz2/i;
  819.       }
  820.       $orig_size = (stat $_)[7];
  821.       ++$counter;
  822.       print "Repacking $_ ($counter/$total)...\n";
  823.       if ((system "gzip -cd $_ |bzip2 >$new") == 0) {
  824.           $new_size = (stat $new)[7];
  825.           $factor = int(100*$new_size/$orig_size+.5);
  826.           $saved_bytes += $orig_size-$new_size;
  827.           print "$new is about $factor% of the size of $_. :",($factor<100)?')':'(',"\n";
  828.           unlink $_;
  829.       } else {
  830.           print "Arrgghh!  Something happened to $_: $!\n";
  831.       }
  832.   }
  833.   print "You've ",
  834.         ($saved_bytes>=0)?"saved":"lost",
  835.         " $saved_bytes bytes of storage space :",
  836.          ($saved_bytes>=0)?")":"(", "\n";
  837.  
  838.   unless (-e '/tmp/machine_bzip2_total') {
  839.       system ('echo "0" >/tmp/machine_bzip2_total');
  840.       system ('chmod', '0666', '/tmp/machine_bzip2_total');
  841.   }
  842.  
  843.  
  844.   chomp($machine_bzip2_total = `cat $totals_file`);
  845.   open TOTAL, ">$totals_file"
  846.        or die "Can't open system-wide total: $!";
  847.   $machine_bzip2_total += $saved_bytes;
  848.   print TOTAL $machine_bzip2_total;
  849.   close TOTAL;
  850.  
  851.   print "That's a machine-wide total of ",`cat $totals_file`," bytes saved.\n";
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.