
                                  Bzip2 Howto
                                       
:
David Fetter, [1]dfetter@best.com
:
л HSIEH KUN-CHUNG, ̨ л Taiwan, Republic of China (NOT People's
Republic of China) (ʼ [2]linuxer.bbs@cis.nctu.edu.tw 
[3]linux4tw@geocities.com ҳ "Linux for Taiwan ̨˵ Linux"
[4]http://members.xoom.com/linux4tw/

   汾 v1.921998 818
     _________________________________________________________________
   
   ļڽʹµ bzip2 ѹʽĿǰ sgml 汾 EURO
   [5] "õ" sgml  [6]
     _________________________________________________________________
   
1. 

     * 1.1 ظʷ
       
2. ȡ bzip2

     * 2.1 Bzip2-HOWTO ԰汾
     * 2.2 ȡ bzip2 δĶλ
     * 2.3 ȡ bzip2 ԭʼʽ
     * 2.4  bzip2 Ļʹ
       
3. ʹ bzip2

4.  tarʹ bzip2

     * 4.1 :
     * 4.2 ʹãҪ root Ȩ:
     * 4.3 ͬʹãҪ root ȡ
       
5. ʹ bzip2  less

6. ʹ bzip2  emacs

     * 6.1 Ϊÿ˸ı emacs:
     * 6.2 Ϊĳ˸ı emacs:
       
7.  bzip2  wu-ftpd

8. ʹ bzip2  grep

9. bzip2  X µ Netscape ʹá

10. ʹ bzip2 ѹѹʽ

11. 뱾
     _________________________________________________________________
   
1. 

   Bzip2 Ǹܰѹ֮㷨ܹﵽĶ gzip
   ĵ֮ 60-70% ҵѹʡ
   
   ļ㼸 bzip2 Ӧ塣
   
   ļδ潫ۼֵ bzip2 ʽ⣬óʽ ԭbzip2 
   [7]Julian Seward :
   
 bzip2 һ汾Ҳͬ .bz2 ʽ
ҪӵĹһ zlib ĳʽ⡣ǿԴӳʽͷȡдϵ
ʽĵ

   ļδ汾ҲǷӦ (Լ) Linux kernel ʹ bzip2֮
   Ľۡ
   
1.1 ظʷ

  v1.92 [8]ȡ bzip2 λʽ ½ڣ S.u.S.E.ġ
  
  v1.91
  
    [9]ʹ bzip2  tar ¡һԼ ŪһЩ shell 
   л Alessandro Rubini
   
    buzzit ߲ȥ stomp ԭ bzip2 ѹ
   
    bgrep,  zgrep-Ĺߡ
   
  v1.9
  
    gcc 2.7.* ⡣л Ulrik Dickow ָ
   
    Leonard Jean-Marc ܰ tar
   
    Linus erlund İ汾
   
    Arnaud Launay Ľ wu-ftpd ½
   
   ķŵԼλ½ϡ
   
  v1.8
  
    buzzit  tar.diff ŵ sgml ļӦñõĵط ƴ
   ʽл Arnaud Launay Эļ:-)
   
   ɱ xv ƻ˴ҲȤ
   
    teasers Ϊļδ汾
   
  v1.7
  
    buzzit óʽ gnu tar ޲
   
  v1.6
  
    TenThumbs  Netscape ߡ
   
   ԼĽ lesspipe.sh ڿԹĸˡ
   
  v1.5
  
    Arnaud Launay ķ뱾Լ wu-ftpd 
   
  v1.4
  
    Tetsu Isaji 뱾
   
  v1.3
  
    Ulrik Dickow  19.30 ߰汾õ .emacs    
   ( emacsõ jka-compr.el ޲! Bzip2 û(?)
   "append" ѡ)
   
  v1.2
  
    emacs ޲Զذʶ .bz2 
   
  v1.1
  
    emacs
   
  v1.0
  
   һغϡ
   
2. ȡ bzip2

   Bzip2 ҳ [10]Ӣվ ӳվ [11]Ҳ
   Red Hat ftp վ [12]ҵ
   
2.1 Bzip2-HOWTO ԰汾

   ˵˿Կ Arnaud Launay ķļ ҳ汾 [13]
    ftp [14]ȡ  [15]ʼArnaud
   
   ˵˿Կ Tetsu Isaji [16]뱾 Isaji [17]
   վ [18]ʼķʽ
   
   ˵ѿԲ Linus erlund 뱾 [19]⡣
   [20]ʼ  Linus 硣
   
2.2 ȡ bzip2 δĶλ

   һվ
   
   Debian Intel λʽ [21]
   
   Red Hat alpha λʽ [22]
   
   Red Hat Intel λʽ [23]
   
   Red Hat SPARC λʽ [24]
   
   Slackware Intel λʽ [25]
   
   S.u.S.E. Intel λʽ [26]
   
   ҲԴƵӳվȡõ
   
2.3 ȡ bzip2 ԭʼʽ

   Դӹٷվ ( [27]Getting Bzip2  Red Hat Ҳ [28])
   
2.4  bzip2 Ļʹ

    gcc 2.7.*
   
CFLAGS = -O3 -fomit-frame-pointer -funroll-loops

   ĳ
   
CFLAGS = -O2 -fomit-frame-pointer

   Ҳǣ -O2 ĳ -O3  -funroll-loopsɾҲԼκڱ
    kernel ʱ -m*  ( -m486)
   
   ʹ -funroll-loops ҪˣΪ gcc 2.7
   ĳʽ룬Լȫ gcc 2.7 ٶֵֽĳʽ롣 ı
    (lcc, egcs, gcc 2.8.x) ʹڶ CFLAGS û⡣
   
   ֮ᣬֻ make Ȼ README װ
   
3. ʹ bzip2

   ĺֲܰ :)
   
4.  tarʹ bzip2

    tar ʹ bzip2 ķҲ
   
4.1 :

   Ͳ趨ִн⿪bzip2'd  tar Ŀ¼
    foo.tar.bz2ִ
   
/path/to/bzip2 -cd foo.tar.bz2 | tar xf -

   ɹˣгСȱ㡣
   
4.2 ʹãҪ root Ȩ:

   л [29]Leonard Jean-Marc СɡҲлл [30]Alessandro
   Rubini  csh  bash ġ
   
    .bashrcԼ:
   
alias btar='tar --use-compress-program /usr/local/bin/bzip2 '

    .tcshrc  .cshrcͬ:
   
alias btar 'tar --use-compress-program /usr/local/bin/bzip2'

4.3 ͬʹãҪ root ȡ

   ޲ gnu tar 1.12
   
cd tar-1.12/src; patch < /path/to/tar.diff

   롢װ׼ˡ"which tar"  "which bzip2"ȷ
    tar  bzip2  $PATH Ҫʹµ tarֻ
   
 tar xyf foo.tar.bz2

   ѹ
   
   Ҫµѹͬ:
   
 tar cyf foo.tar.bz2 1 2 3...Ŀ¼1 Ŀ¼2...

   ޲ĵط:
*** tar.c       Thu Jun 11 00:09:23 1998
--- tar.c.new   Thu Jun 11 00:14:24 1998
***************
*** 196,201 ****
--- 196,203 ----
    {"block-number", no_argument, NULL, 'R'},
    {"block-size", required_argument, NULL, OBSOLETE_BLOCKING_FACTOR},
    {"blocking-factor", required_argument, NULL, 'b'},
+   {"bzip2", required_argument, NULL, 'y'},
+   {"bunzip2", required_argument, NULL, 'y'},
    {"catenate", no_argument, NULL, 'A'},
    {"checkpoint", no_argument, &checkpoint_option, 1},
    {"compare", no_argument, NULL, 'd'},
***************
*** 372,377 ****
--- 374,380 ----
                PATTERN                at list/extract time, a globbing PATTERN
\n\
    -o, --old-archive, --portability   write a V7 format archive\n\
        --posix                        write a POSIX conformant archive\n\
+   -y, --bzip2, --bunzip2             filter the archive through bzip2\n\
    -z, --gzip, --ungzip               filter the archive through gzip\n\
    -Z, --compress, --uncompress       filter the archive through compress\n\
        --use-compress-program=PROG    filter through PROG (must accept -d)\n")
,
***************
*** 448,454 ****
     Y  per-block gzip compression */

  #define OPTION_STRING \
!   "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"

  static void
  set_subcommand_option (enum subcommand subcommand)
--- 451,457 ----
     Y  per-block gzip compression */

  #define OPTION_STRING \
!   "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxyz"

  static void
  set_subcommand_option (enum subcommand subcommand)
***************
*** 805,810 ****
--- 808,817 ----
        case 'X':
        exclude_option = 1;
        add_exclude_file (optarg);
+       break;
+
+       case 'y':
+       set_use_compress_program_option ("bzip2");
        break;

        case 'z':

5. ʹ bzip2  less

   Ͻѹ bzip2 磬ڲ bunzip2 ǰ¼ "less"
   ϣ lesspipe.sh (man less)棺
#!/bin/sh
# This is a preprocessor for 'less'.  It is used when this environment
# variable is set:   LESSOPEN="|lesspipe.sh %s"

  case "$1" in
  *.tar) tar tvvf $1 2>/dev/null ;; # View contents of various tar'd files
  *.tgz) tar tzvvf $1 2>/dev/null ;;
# This one work for the unmodified version of tar:
  *.tar.bz2) bzip2 -cd $1 $1 2>/dev/null | tar tzvvf - ;;
#This one works with the patched version of tar:
# *.tar.bz2) tyvvf $1 2>/dev/null ;;
  *.tar.gz) tar tzvvf $1 2>/dev/null ;;
  *.tar.Z) tar tzvvf $1 2>/dev/null ;;
  *.tar.z) tar tzvvf $1 2>/dev/null ;;
  *.bz2) bzip2 -dc $1  2>/dev/null ;; # View compressed files correctly
  *.Z) gzip -dc $1  2>/dev/null ;;
  *.z) gzip -dc $1  2>/dev/null ;;
  *.gz) gzip -dc $1  2>/dev/null ;;
  *.zip) unzip -l $1 2>/dev/null ;;
  *.1|*.2|*.3|*.4|*.5|*.6|*.7|*.8|*.9|*.n|*.man) FILE=`file -L $1` ; # groff sr
c
    FILE=`echo $FILE | cut -d ' ' -f 2`
    if [ "$FILE" = "troff" ]; then
      groff -s -p -t -e -Tascii -mandoc $1
    fi ;;
  *) cat $1 2>/dev/null ;;
#  *) FILE=`file -L $1` ; # Check to see if binary, if so -- view with 'strings
'
#    FILE1=`echo $FILE | cut -d ' ' -f 2`
#    FILE2=`echo $FILE | cut -d ' ' -f 3`
#    if [ "$FILE1" = "Linux/i386" -o "$FILE2" = "Linux/i386" \
#         -o "$FILE1" = "ELF" -o "$FILE2" = "ELF" ]; then
#      strings $1
#    fi ;;
  esac

6. ʹ bzip2  emacs

6.1 Ϊÿ˸ı emacs:

   дĸ jka-compr.el õ޲ bzip2 ΪԶѹģʽ
   
   : ֻ emacs-20.2ԹҲҲ ɶ
   ͬķȴ汾ʹõġ
   
   ʹ
    1.  emacs-20.2/lisp ԭʼʽĿ¼ (Ķ untarred )
    2. ޲ɽ jka-compr.el.diff ĵ(ȫ⵵ )
    3. Ȼִ
       
 patch < jka-compr.el.diff

    4.  emacsִ
       
 M-x byte-compile-file jka-compr.el

    5. 뿪 emacs
    6. ԭ jka-compr.elc ŵȫĵطԷгʱá
    7. µ jka-compr.elc
    8. ú!
       
--- jka-compr.el        Sat Jul 26 17:02:39 1997
+++ jka-compr.el.new    Thu Feb  5 17:44:35 1998
@@ -44,7 +44,7 @@
 ;; The variable, jka-compr-compression-info-list can be used to
 ;; customize jka-compr to work with other compression programs.
 ;; The default value of this variable allows jka-compr to work with
-;; Unix compress and gzip.
+;; Unix compress and gzip.  David Fetter added bzip2 support :)
 ;;
 ;; If you are concerned about the stderr output of gzip and other
 ;; compression/decompression programs showing up in your buffers, you
@@ -121,7 +121,9 @@


 ;;; I have this defined so that .Z files are assumed to be in unix
-;;; compress format; and .gz files, in gzip format.
+;;; compress format; and .gz files, in gzip format, and .bz2 files,
+;;; in the snappy new bzip2 format from http://www.muraroa.demon.co.uk.
+;;; Keep up the good work, people!
 (defcustom jka-compr-compression-info-list
   ;;[regexp
   ;; compr-message  compr-prog  compr-args
@@ -131,6 +133,10 @@
      "compressing"    "compress"     ("-c")
      "uncompressing"  "uncompress"   ("-c")
      nil t]
+    ["\\.bz2\\'"
+     "bzip2ing"        "bzip2"         ("")
+     "bunzip2ing"      "bzip2"         ("-d")
+     nil t]
     ["\\.tgz\\'"
      "zipping"        "gzip"         ("-c" "-q")
      "unzipping"      "gzip"         ("-c" "-q" "-d")

6.2 Ϊĳ˸ı emacs:

   Ҫлл Ulrik Dickow, [31]ukd@kampsax.dkKampsax Technologyϵ
   ͳʽʦ :
   
   Ϊ÷ sysadmin ѿԶʹ bzip2ֻ .emacs 
   ж
   
;; Automatic (un)compression on loading/saving files (gzip(1) and similar)
;; We start it in the off state, so that bzip2(1) support can be added.
;; Code thrown together by Ulrik Dickow for ~/.emacs with Emacs 19.34.
;; Should work with many older and newer Emacsen too.  No warranty though.
;;
(if (fboundp 'auto-compression-mode) ; Emacs 19.30+
    (auto-compression-mode 0)
  (require 'jka-compr)
  (toggle-auto-compression 0))
;; Now add bzip2 support and turn auto compression back on.
(add-to-list 'jka-compr-compression-info-list
             ["\\.bz2\\(~\\|\\.~[0-9]+~\\)?\\'"
              "zipping"        "bzip2"         ()
              "unzipping"      "bzip2"         ("-d")
              nil t])
(toggle-auto-compression 1 t)

7.  bzip2  wu-ftpd

   л Arnaud Launay ʡƵؾӦǷŵ
   /etc/ftpconversions  bzip2 ʱѹѹ ȷ·
    ( /bin/compress) Ҫȷ
   
 :.Z:  :  :/bin/compress -d -c %s:T_REG|T_ASCII:O_UNCOMPRESS:UNCOMPRESS
 :   : :.Z:/bin/compress -c %s:T_REG:O_COMPRESS:COMPRESS
 :.gz: :  :/bin/gzip -cd %s:T_REG|T_ASCII:O_UNCOMPRESS:GUNZIP
 :   : :.gz:/bin/gzip -9 -c %s:T_REG:O_COMPRESS:GZIP
 :.bz2: :  :/bin/bzip2 -cd %s:T_REG|T_ASCII:O_UNCOMPRESS:BUNZIP2
 :   : :.bz2:/bin/bzip2 -9 -c %s:T_REG:O_COMPRESS:BZIP2
 :   : :.tar:/bin/tar -c -f - %s:T_REG|T_DIR:O_TAR:TAR
 :   : :.tar.Z:/bin/tar -c -Z -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+COMPRESS
 :   : :.tar.gz:/bin/tar -c -z -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+GZIP
 :   : :.tar.bz2:/bin/tar -c -y -f - %s:T_REG|T_DIR:O_COMPRESS|O_TAR:TAR+BZIP2

8. ʹ bzip2  grep

   Ĺóʽҹ bgrepʵǰѰ Linux  zgrep С
    ѡʹ grep bunzip2 
   
#!/bin/sh

# bgrep -- a wrapper around a grep program that decompresses files as needed
PATH="/usr/bin:$PATH"; export PATH

prog=`echo $0 | sed 's|.*/||'`
case "$prog" in
        *egrep) grep=${EGREP-egrep}     ;;
        *fgrep) grep=${FGREP-fgrep}     ;;
        *)      grep=${GREP-grep}       ;;
esac
pat=""
while test $# -ne 0; do
  case "$1" in
  -e | -f) opt="$opt $1"; shift; pat="$1"
           if test "$grep" = grep; then  # grep is buggy with -e on SVR4
             grep=egrep
           fi;;
  -*)      opt="$opt $1";;
   *)      if test -z "$pat"; then
             pat="$1"
           else
             break;
           fi;;
  esac
  shift
done

if test -z "$pat"; then
  echo "grep through bzip2 files"
  echo "usage: $prog [grep_options] pattern [files]"
  exit 1
fi

list=0
silent=0
op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
case "$op" in
  *l*) list=1
esac
case "$op" in
  *h*) silent=1
esac

if test $# -eq 0; then
  bzip2 -cd | $grep $opt "$pat"
  exit $?
fi

res=0
for i do
  if test $list -eq 1; then
    bzip2 -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
    r=$?
  elif test $# -eq 1 -o $silent -eq 1; then
    bzip2 -cd "$i" | $grep $opt "$pat"
    r=$?
  else
    bzip2 -cd "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
    r=$?
  fi
  test "$r" -ne 0 && res="$r"
done
exit $res

9. bzip2  X µ Netscape ʹá

   tenthumbs@cybernex.net ˵:
   
     ҵһ Linux Netscape ʹ bzip2 Ϊ Content-Encoding
     ͬʹ gzip һĶӵ $HOME/.Xdefaults 
     $HOME/.Xresources
     
     ʹ -s ѡΪѡѹٶ RAM ռá 
     ԼҪġ
     
Netscape*encodingFilters:      \
        x-compress :  : .Z     : uncompress -c  \n\
        compress   :  : .Z     : uncompress -c  \n\
        x-gzip     :  : .z,.gz : gzip -cdq      \n\
        gzip       :  : .z,.gz : gzip -cdq      \n\
        x-bzip2    :  : .bz2   : bzip2 -ds \n

10. ʹ bzip2 ѹѹʽ

    perl ʽеѹʽ (.tar.gz, .tgz. .tar.Z,  .Z
   for this iteration) ´ѵѹֵ perl ԭʼʽȫλ
   ĺļ ˵ʲԼΰ쵽ġ
#!/usr/bin/perl -w

#######################################################
#                                                     #
# This program takes compressed and gzipped programs  #
# in the current directory and turns them into bzip2  #
# format.  It handles the .tgz extension in a         #
# reasonable way, producing a .tar.bz2 file.          #
#                                                     #
#######################################################
$counter = 0;
$saved_bytes = 0;
$totals_file = '/tmp/machine_bzip2_total';
$machine_bzip2_total = 0;

while(<*[Zz]>) {
    next if /^bzip2-0.1pl2.tar.gz$/;
    push @files, $_;
}
$total = scalar(@files);

foreach (@files) {
    if (/tgz$/) {
        ($new=$_) =~ s/tgz$/tar.bz2/;
    } else {
        ($new=$_) =~ s/\.g?z$/.bz2/i;
    }
    $orig_size = (stat $_)[7];
    ++$counter;
    print "Repacking $_ ($counter/$total)...\n";
    if ((system "gzip -cd $_ |bzip2 >$new") == 0) {
        $new_size = (stat $new)[7];
        $factor = int(100*$new_size/$orig_size+.5);
        $saved_bytes += $orig_size-$new_size;
        print "$new is about $factor% of the size of $_. :",($factor<100)?')':'
(',"\n";
        unlink $_;
    } else {
        print "Arrgghh!  Something happened to $_: $!\n";
    }
}
print "You've ",
      ($saved_bytes>=0)?"saved":"lost",
      " $saved_bytes bytes of storage space :",
       ($saved_bytes>=0)?")":"(", "\n";

unless (-e '/tmp/machine_bzip2_total') {
    system ('echo "0" >/tmp/machine_bzip2_total');
    system ('chmod', '0666', '/tmp/machine_bzip2_total');
}


chomp($machine_bzip2_total = `cat $totals_file`);
open TOTAL, ">$totals_file"
     or die "Can't open system-wide total: $!";
$machine_bzip2_total += $saved_bytes;
print TOTAL $machine_bzip2_total;
close TOTAL;

print "That's a machine-wide total of ",`cat $totals_file`," bytes saved.\n";

11. 뱾

   ע: ֱļз벻˳ĵطŸң
   лл ߵʼ [32]linuxer.bbs@cis.nctu.edu.tw 
   [33]linux4tw@geocities.com
   
   ע HOWTO ļĮ̇ Linux ʹ֯µ Linux 
   ƻ [34]http://www.linux.org.tw/CLDP/
   "Linux for Taiwan ̨˵ Linux "
   [35]http://members.xoom.com/linux4tw/ ȡá

References

   1. mailto:dfetter@best.com
   2. mailto:linuxer.bbs@cis.nctu.edu.tw
   3. mailto:linux4tw@geocities.com
   4. http://members.xoom.com/linux4tw/
   5. file://localhost/tmp/zh-sgmltools.24769/Bzip2-HOWTO.sgml
   6. http://www.best.com/~dfetter/Bzip2-HOWTO/Bzip2-HOWTO.sgml
   7. mailto:Julian_Seward@muraroa.demon.co.uk
   8. file://localhost/tmp/zh-sgmltools.24769/Bzip2.txt.html#bzip2-binaries
   9. file://localhost/tmp/zh-sgmltools.24769/Bzip2.txt.html#bzip2-with-tar
  10. http://www.muraroa.demon.co.uk/
  11. http://www.digistar.com/bzip2/index.html
  12. ftp://ftp.redhat.com/pub/contrib
  13. http://www.freenix.fr/linux/HOWTO/mini/Bzip2.html
  14. ftp://ftp.lip6.fr/pub/linux/french/docs/HOWTO/mini/Bzip2.gz
  15. mailto:zoro@mygale.org
  16. http://jf.gee.kyoto-u.ac.jp/JF/JF.html
  17. http://www2s.biglobe.ne.jp/~kaien/
  18. mailto:isaji@mxu.meshnet.or.jp
  19. http://user.tninet.se/~uxm165t/linux_doc.html
  20. mailto:uxm165t@tninet.se
  21. ftp://ftp.debian.org/debian/dists/stable/main/binary-i386/utils/bzip2_0.1pl2-5.deb
  22. ftp://ftp.redhat.com/pub/redhat/redhat-5.1/alpha/RedHat/RPMS/bzip2-0.1pl2-1.alpha.rpm
  23. ftp://ftp.redhat.com/pub/redhat/redhat-5.1/i386/RedHat/RPMS/bzip2-0.1pl2-1.i386.rpm
  24. ftp://ftp.redhat.com/pub/redhat/redhat-5.1/sparc/RedHat/RPMS/bzip2-0.1pl2-1.sparc.rpm
  25. ftp://www.cdrom.com/pub/linux/slackware-3.5/slakware/a1/bzip2.tgz
  26. ftp://ftp.suse.com/pub/SuSE-Linux/5.2/suse/ap1/bzip.rpm
  27. file://localhost/tmp/zh-sgmltools.24769/Bzip2.txt.html#get-bzip2
  28. ftp://ftp.redhat.com/pub/contrib/SRPMS/bzip2-0.1pl2-1.src.rpm
  29. mailto:leonard@sct1.is.belgacom.be
  30. mailto:rubini@morgana.systemy.it
  31. mailto:ukdATkampsax.dk
  32. mailto:linuxer.bbs@cis.nctu.edu.tw
  33. mailto:linux4tw@geocities.com
  34. http://www.linux.org.tw/CLDP/
  35. http://members.xoom.com/linux4tw/
