Bzip2 Howto

资讯 驱动中国 752 次阅读 0 条评论
分享 Q

作者:DavidFetter,dfetter@best.com
译者:谢昆中HSIEHKUN-CHUNG,(电子邮件linuxer.bbs@cis.nctu.edu.tw或linux4tw@geocities.com网页"LinuxforTaiwan台湾人的Linux"http://members.xoom.com/linux4tw/

版本v1.92,1998年8月18日


--------------------------------------------------------------------------------
这份文件在教你如何使用新的bzip2压缩程式。目前的sgml版本被放在EURO这□,还有"作者用的"sgml档放在http://www.best.com/~dfetter/Bzip2-HOWTO/Bzip2-HOWTO.sgml。
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

1.简介
Bzip2是个很棒的压缩资料用之新型演算法。大致上能够达到以它的对手gzip所产出的档案之60-70%左右的压缩率。


这份文件将教你几个bzip2的应用软体。


本文件的未来版将会讨论即将出现的bzip2程式库,该程式库原bzip2的作者JulianSeward描述如下:


现在正致力於bzip2的下一个版本,它也将采用相同的.bz2档案格式;
主要增加的功能是一个像zlib的程式库。让我们可以从程式□头来读取和书写资料到这种格式的档案□。


本文件的未来版本也会包含是否应该(以及如何)让Linuxkernel使用bzip2之议题的结论。


1.1沿革历史
v1.92更新取得bzip2二进位程式码章节,加入S.u.S.E.的。
v1.91
在使用bzip2配合tar那章□修正一个□例以及弄清楚一些shell句子。感谢AlessandroRubini。


更新buzzit工具不会去stomp原来的bzip2压缩档。


新增bgrep,像zgrep-的工具。


v1.9
澄清gcc2.7.*的问题。感谢UlrikDickow的热心指出。


加入LeonardJean-Marc很棒的配合tar


加上Linus攀erlund的瑞典文版本。


依照ArnaudLaunay的建议修正了wu-ftpd章节


把译文放到它们自己的位章节上。


v1.8
把buzzit和tar.diff放到sgml文件□它们应该被放置的地方。修正拼字与格式。感谢ArnaudLaunay的协助我修正这份文件。:-)


杀掉xv计画,因此大家不感兴趣。


加上teasers为本文件的未来版本。


v1.7
加入buzzit公用程式。修正对gnutar的修补。


v1.6
加入TenThumbs的Netscape致能者。


以及依据它的建议更改lesspipe.sh。它现在可以工作的更好了。


v1.5
加入ArnaudLaunay的法文译本,以及他的wu-ftpd档。


v1.4
加入TetsuIsaji的日文译本。


v1.3
加入UlrikDickow给19.30或更高版本用的.emacs档□


(还有修正给emacs用的jka-compr.el修补档。糟了!Bzip2还没有(?)"append"的旗标选项。)


v1.2
加入对emacs的修补,让它可以自动地办识出.bz2档。


v1.1
加入对emacs


v1.0
第一回合。




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

2.取得bzip2
Bzip2的主网页在英国的主网站http://www.muraroa.demon.co.uk/。美国的映射站在这http://www.digistar.com/bzip2/index.html。你也可以在RedHat的ftp站□ftp://ftp.redhat.com/pub/contrib。


2.1Bzip2-HOWTO各种语言版本
说法语的人可以看看ArnaudLaunay的法文文件。网页版本在这□http://www.freenix.fr/linux/HOWTO/mini/Bzip2.html,你可以用ftp来取得ftp://ftp.lip6.fr/pub/linux/french/docs/HOWTO/mini/Bzip2.gz你可以用电子邮件来连络Arnaud


说日语的人可以看看TetsuIsaji的日文译本。你可以用上Isaji的网站,或是用电子邮件的方式。



说瑞典语的朋友可以参阅Linus攀erlund的瑞典文译本按这。你可以用电子邮件与Linus连络。


2.2取得bzip2未编译的二进位档
查一下主网站。


Debian的Intel二进位程式码在这□ftp://ftp.debian.org/debian/dists/stable/main/binary-i386/utils/bzip2_0.1pl2-5.deb。


RedHat的alpha二进位程式码在这□ftp://ftp.redhat.com/pub/redhat/redhat-5.1/alpha/RedHat/RPMS/bzip2-0.1pl2-1.alpha.rpm。


RedHat的Intel二进位程式码在这□ftp://ftp.redhat.com/pub/redhat/redhat-5.1/i386/RedHat/RPMS/bzip2-0.1pl2-1.i386.rpm。


RedHat的SPARC二进位程式码在这□ftp://ftp.redhat.com/pub/redhat/redhat-5.1/sparc/RedHat/RPMS/bzip2-0.1pl2-1.sparc.rpm。


Slackware的Intel二进位程式码在这□ftp://www.cdrom.com/pub/linux/slackware-3.5/slakware/a1/bzip2.tgz。


S.u.S.E.的Intel二进位程式码在这□ftp://ftp.suse.com/pub/SuSE-Linux/5.2/suse/ap1/bzip.rpm。

你也可以从许多相类似的映射站得取得档案。


2.3取得bzip2原始程式
你可以从官方的网站(看GettingBzip2,或是RedHat也有它ftp://ftp.redhat.com/pub/contrib/SRPMS/bzip2-0.1pl2-1.src.rpm)。


2.4编译bzip2给你的机器使用
如果你有gcc2.7.*,将下面这行

CFLAGS=-O3-fomit-frame-pointer-funroll-loops

改成

CFLAGS=-O2-fomit-frame-pointer

也就是,把-O2改成-O3并把-funroll-loops删掉。你也可以加入任何你在编译kernel时的-m*旗标(例如像-m486)。

避免使用-funroll-loops是最重要的了,因为它会造成许多的gcc2.7产生错误的程式码,以及全部的gcc2.7产生速度慢又较胖的程式码。对其他的编译器(lcc,egcs,gcc2.8.x)使用内定的CFLAGS就没问题。

之後,只需make它,然後依照README来安装它。



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

3.使用bzip2
请参阅很棒的手册:)



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

4.配合tar来使用bzip2
下面有三种配合tar使用bzip2的方法,也就是

4.1容易设立:
这个方法跟本就不必做设定。执行解开bzip2'd的tar包裹,在现行目录底下有个foo.tar.bz2,执行

/path/to/bzip2-cdfoo.tar.bz2|tarxf-

成功了,但可能有常需输入的小缺点。


4.2容易设立,容易使用,不需要root的特权:
感谢LeonardJean-Marc贡献这个小技巧。也谢谢AlessandroRubini从csh□分类出bash的。


在你的.bashrc□,你可以加入像下面这行:

aliasbtar='tar--use-compress-program/usr/local/bin/bzip2'


在你的.tcshrc或.cshrc□,可用下面这行相同的:

aliasbtar'tar--use-compress-program/usr/local/bin/bzip2'


4.3同样是容易使用,但需要root来存取。
用下面来方法来修补gnutar1.12:

cdtar-1.12/src;patch</path/to/tar.diff

编译、安装它,你已准备就绪了。用"whichtar"和"whichbzip2"来确认这两个tar和bzip2有在你的$PATH□。要使用新的tar,只需
tarxyffoo.tar.bz2

来压缩档案。
要制作新的压缩档,同样地:

tarcyffoo.tar.bz2档名1档名2档名3...目录1目录2...

下面是修补的地方:

***tar.cThuJun1100:09:231998
---tar.c.newThuJun1100:14:241998
***************
***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----
PATTERNatlist/extracttime,aglobbingPATTERN\n\
-o,--old-archive,--portabilitywriteaV7formatarchive\n\
--posixwriteaPOSIXconformantarchive\n\
+-y,--bzip2,--bunzip2filterthearchivethroughbzip2\n\
-z,--gzip,--ungzipfilterthearchivethroughgzip\n\
-Z,--compress,--uncompressfilterthearchivethroughcompress\n\
--use-compress-program=PROGfilterthroughPROG(mustaccept-d)\n"),
***************
***448,454****
Yper-blockgzipcompression*/

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

staticvoid
set_subcommand_option(enumsubcommandsubcommand)
---451,457----
Yper-blockgzipcompression*/

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

staticvoid
set_subcommand_option(enumsubcommandsubcommand)
***************
***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(manless),像下面:

#!/bin/sh
#Thisisapreprocessorfor'less'.Itisusedwhenthisenvironment
#variableisset:LESSOPEN="|lesspipe.sh%s"

case"$1"in
*.tar)tartvvf$12>/dev/null;;#Viewcontentsofvarioustar'dfiles
*.tgz)tartzvvf$12>/dev/null;;
#Thisoneworkfortheunmodifiedversionoftar:
*.tar.bz2)bzip2-cd$1$12>/dev/null|tartzvvf-;;
#Thisoneworkswiththepatchedversionoftar:
#*.tar.bz2)tyvvf$12>/dev/null;;
*.tar.gz)tartzvvf$12>/dev/null;;
*.tar.Z)tartzvvf$12>/dev/null;;
*.tar.z)tartzvvf$12>/dev/null;;
*.bz2)bzip2-dc$12>/dev/null;;#Viewcompressedfilescorrectly
*.Z)gzip-dc$12>/dev/null;;
*.z)gzip-dc$12>/dev/null;;
*.gz)gzip-dc$12>/dev/null;;
*.zip)unzip-l$12>/dev/null;;
*.1|*.2|*.3|*.4|*.5|*.6|*.7|*.8|*.9|*.n|*.man)FILE=`file-L$1`;#groffsrc
FILE=`echo$FILE|cut-d''-f2`
if["$FILE"="troff"];then
groff-s-p-t-e-Tascii-mandoc$1
fi;;
*)cat$12>/dev/null;;
#*)FILE=`file-L$1`;#Checktoseeifbinary,ifso--viewwith'strings'
#FILE1=`echo$FILE|cut-d''-f2`
#FILE2=`echo$FILE|cut-d''-f3`
#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上试过这个,但是我也找不到有啥麽理由来相信同样的方法却不能在其他版本上使用的。

使用它,

到emacs-20.2/lisp原始程式码目录□(看你在哪儿untarred它的)
把下面的修补做成叫jka-compr.el.diff的档案(全放在这档案□)。
然後执行
patch<jka-compr.el.diff

启动emacs,再执行
M-xbyte-compile-filejka-compr.el

离开emacs。
把它原来的jka-compr.elc放到安全的地方以防有臭虫时备用。
用新的jka-compr.elc来代替它。
好好玩吧!
---jka-compr.elSatJul2617:02:391997
+++jka-compr.el.newThuFeb517:44:351998
@@-44,7+44,7@@
;;Thevariable,jka-compr-compression-info-listcanbeusedto
;;customizejka-comprtoworkwithothercompressionprograms.
;;Thedefaultvalueofthisvariableallowsjka-comprtoworkwith
-;;Unixcompressandgzip.
+;;Unixcompressandgzip.DavidFetteraddedbzip2support:)
;;
;;Ifyouareconcernedaboutthestderroutputofgzipandother
;;compression/decompressionprogramsshowingupinyourbuffers,you
@@-121,7+121,9@@


;;;Ihavethisdefinedsothat.Zfilesareassumedtobeinunix
-;;;compressformat;and.gzfiles,ingzipformat.
+;;;compressformat;and.gzfiles,ingzipformat,and.bz2files,
+;;;inthesnappynewbzip2formatfromhttp://www.muraroa.demon.co.uk.
+;;;Keepupthegoodwork,people!
(defcustomjka-compr-compression-info-list
;;[regexp
;;compr-messagecompr-progcompr-args
@@-131,6+133,10@@
"compressing""compress"("-c")
"uncompressing""uncompress"("-c")
nilt]
+["\\.bz2\\'"
+"bzip2ing""bzip2"("")
+"bunzip2ing""bzip2"("-d")
+nilt]
["\\.tgz\\'"
"zipping""gzip"("-c""-q")
"unzipping""gzip"("-c""-q""-d")


6.2为某人改变emacs:
这次要谢谢UlrikDickow,ukd@kampsax.dk,他是KampsaxTechnology的系统程式设计师:

为了让非sysadmin的朋友可以自动地使用bzip2,只需在你的.emacs档案□加上下列东东。


;;Automatic(un)compressiononloading/savingfiles(gzip(1)andsimilar)
;;Westartitintheoffstate,sothatbzip2(1)supportcanbeadded.
;;CodethrowntogetherbyUlrikDickowfor~/.emacswithEmacs19.34.
;;ShouldworkwithmanyolderandnewerEmacsentoo.Nowarrantythough.
;;
(if(fboundp'auto-compression-mode);Emacs19.30+
(auto-compression-mode0)
(require'jka-compr)
(toggle-auto-compression0))
;;Nowaddbzip2supportandturnautocompressionbackon.
(add-to-list'jka-compr-compression-info-list
["\\.bz2\\(~\\|\\.~[0-9]+~\\)?\\'"
"zipping""bzip2"()
"unzipping""bzip2"("-d")
nilt])
(toggle-auto-compression1t)



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

7.用bzip2来配合wu-ftpd
感谢ArnaudLaunay这则节省频宽的秘诀。下面的内容应该是放到/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--awrapperaroundagrepprogramthatdecompressesfilesasneeded
PATH="/usr/bin:$PATH";exportPATH

prog=`echo$0|sed's|.*/||'`
case"$prog"in
*egrep)grep=${EGREP-egrep};;
*fgrep)grep=${FGREP-fgrep};;
*)grep=${GREP-grep};;
esac
pat=""
whiletest$#-ne0;do
case"$1"in
-e|-f)opt="$opt$1";shift;pat="$1"
iftest"$grep"=grep;then#grepisbuggywith-eonSVR4
grep=egrep
fi;;
-*)opt="$opt$1";;
*)iftest-z"$pat";then
pat="$1"
else
break;
fi;;
esac
shift
done

iftest-z"$pat";then
echo"grepthroughbzip2files"
echo"usage:$prog[grep_options]pattern[files]"
exit1
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

iftest$#-eq0;then
bzip2-cd|$grep$opt"$pat"
exit$?
fi

res=0
forido
iftest$list-eq1;then
bzip2-cdfq"$i"|$grep$opt"$pat">/dev/null&&echo$i
r=$?
eliftest$#-eq1-o$silent-eq1;then
bzip2-cd"$i"|$grep$opt"$pat"
r=$?
else
bzip2-cd"$i"|$grep$opt"$pat"|sed"s|^|${i}:|"
r=$?
fi
test"$r"-ne0&&res="$r"
done
exit$res



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

9.bzip2配合X下的Netscape来使用。
tenthumbs@cybernex.net说:


我找到一个可以让LinuxNetscape来使用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,和.Zforthisiteration)重新打包成最佳的压缩值。这perl原始程式都有全方位的好文件来说明它做什麽以及它如何办到的。

#!/usr/bin/perl-w

#######################################################
##
#Thisprogramtakescompressedandgzippedprograms#
#inthecurrentdirectoryandturnsthemintobzip2#
#format.Ithandlesthe.tgzextensionina#
#reasonableway,producinga.tar.bz2file.#
##
#######################################################
$counter=0;
$saved_bytes=0;
$totals_file='/tmp/machine_bzip2_total';
$machine_bzip2_total=0;

while(<*[Zz]>){
nextif/^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"$newisabout$factor%ofthesizeof$_.:",($factor<100)?')':'(',"\n";
unlink$_;
}else{
print"Arrgghh!Somethinghappenedto$_:$!\n";
}
}
print"You've",
($saved_bytes>=0)?"saved":"lost",
"$saved_bytesbytesofstoragespace:",
($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`);
openTOTAL,">$totals_file"
ordie"Can'topensystem-widetotal:$!";
$machine_bzip2_total+=$saved_bytes;
printTOTAL$machine_bzip2_total;
closeTOTAL;

print"That'samachine-widetotalof",`cat$totals_file`,"bytessaved.\n";



--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

11.中译本
译者注:若您发现本文件内容有翻译不顺畅或错误的地方,请您来信告诉我,以做更正,谢谢。译者电子邮件linuxer.bbs@cis.nctu.edu.tw或linux4tw@geocities.com

译注:这份HOWTO文件的中文版可在台湾Linux使用者组织下的中文Linux文件计画http://www.linux.org.tw/CLDP/
或是"LinuxforTaiwan台湾人的Linux"http://members.xoom.com/linux4tw/下取得。


--------------------------------------------------------------------------------

版权声明:本文由驱动中国整理发布,转载需注明出处与原文链接。如有疑问请联系 editor@qudong.com
本文价值 成为第一个评分的人
登录后为本文打分
网友评论0 条评论
正在回复 的评论取消
评论加载中…
🔐

登录后参与互动

评论、点赞均可赚积分
连续签到、邀请好友也有奖励 🎁

电脑端: 微信扫码登录 微信内: 一键登录

微信扫一扫

打开微信「扫一扫」,分享本文到朋友圈或好友