bzip2 中文man頁面
NAME 命令名
bzip2, bunzip2 - 一種塊排序文件壓縮軟件,v0.9.5
bzcat - 將文件解壓縮至標(biāo)準(zhǔn)輸出
bzip2recover - 恢復(fù)損壞的 bzip2 文件
總覽
bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ]
bunzip2 [ -fkvsVL ] [ filenames ... ]
bzcat [ -s ] [ filenames ... ]
bzip2recover filename
描述
bzip2 采用 Burrows-Wheeler 塊排序文本壓縮算法和 Huffman 編碼方式壓縮文件。壓縮率一般比基于 LZ77/LZ78 的壓縮軟件好得多,其性能接近 PPM 族統(tǒng)計(jì)類壓縮軟件。
命令行參數(shù)有意設(shè)計(jì)為非常接近 GNU gzip 的形式,但也不完全相同。
bzip2 從命令行讀入文件名和參數(shù)。 每個(gè)文件被名為 "原始文件名.bz2" 的壓縮文件替換。每個(gè)壓縮文件具有與原文件相同的修改時(shí)間、 權(quán)限, 如果可能的話,還具有相同的屬主, 因此在解壓縮時(shí)這些特性將正確地恢復(fù)。在某些文件系統(tǒng)中, 沒有權(quán)限、 屬主或時(shí)間的概念,或者對(duì)文件名的長度有嚴(yán)格限制, 例如 MSDOS, 在這種情況下,bzip2 沒有保持原文件名、 屬主、 權(quán)限以及時(shí)間的機(jī)制,從這個(gè)意義上說,bzip2 對(duì)文件名的處理是幼稚的。
bzip2 和 bunzip2 在缺省情況下不覆蓋已有的文件。 如果想覆蓋已有的文件,要指定 -f 選項(xiàng)。
如果未指定文件名, bzip2 將壓縮來自標(biāo)準(zhǔn)輸入的數(shù)據(jù)并寫往標(biāo)準(zhǔn)輸出。在這種情況下, bzip2 會(huì)拒絕將壓縮結(jié)果寫往終端,因?yàn)檫@完全無法理解并且是沒有意義的。
bunzip2 (以及 bzip2 -d) 對(duì)所有指定的文件進(jìn)行解壓縮處理。不是由 bzip2 產(chǎn)生的文件將被忽略,同時(shí)發(fā)出一個(gè)警告信息。 bzip2 按下列方式由壓縮文件名確定解壓后的文件名:
filename.bz2 解壓成 filename
filename.bz 解壓成 filename
filename.tbz2 解壓成 filename.tar
filename.tbz 解壓成 filename.tar
anyothername 解壓成 anyothername.out
如果文件名的后綴不是下列之一: .bz2, .bz, .tbz2 或 .tbz, .bzip2 將抱怨無法確定原始文件名,并采用原文件名加 .out 作為解壓縮文件名。
在壓縮時(shí),如果不提供文件名,bzip2 將從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),壓縮結(jié)果寫往標(biāo)準(zhǔn)輸出。
bunzip2 能夠正確地解壓由兩個(gè)或更多個(gè)壓縮文件連在一起的文件。解壓的結(jié)果為相應(yīng)的連在一起的未壓縮文件。
bzip2 也支持對(duì)連在一起的壓縮文件的完整性檢查(-t選項(xiàng))。
同樣可采用 -c 選項(xiàng)將文件壓縮或解壓縮至標(biāo)準(zhǔn)輸出。多個(gè)文件可通過這種方式壓縮或解壓縮。輸出結(jié)果被依次送往標(biāo)準(zhǔn)輸出。 采用這種方式對(duì)多個(gè)文件的壓縮將生成包含多個(gè)壓縮文件的數(shù)據(jù)流。這樣的數(shù)據(jù)流只能被 0.9.0 版或其后續(xù)版本的 bzip2 正確解壓。較早版本的 bzip2 會(huì)在解壓完第一個(gè)文件之后停止。
bzcat (或 bzip2 -dc) 將所有指定文件解壓縮至標(biāo)準(zhǔn)輸出。
bzip2 可從環(huán)境變量 BZIP2 和 BZIP 中依次讀取參數(shù), 并在命令行參數(shù)之前對(duì)其進(jìn)行處理。 這是提供缺省選項(xiàng)的方便途徑。
即使壓縮后的文件略大于原文件, 壓縮也總是照樣進(jìn)行。小于大約 100 字節(jié)的文件壓縮后傾向于變大,因?yàn)闀?huì)有一個(gè) 50 字節(jié)的數(shù)據(jù)頭。 對(duì)于隨機(jī)數(shù)據(jù) (包括大多數(shù)壓縮軟件的輸出), 大約每字節(jié)壓成 8.05 位, 放大率約為 0.5%。
bzip2 采用 32 位 CRC 校驗(yàn)碼作自我檢查,以確認(rèn)解壓后的文件與原始文件相同。這可用于檢測(cè)壓縮文件是否損壞,并防止 bzip2 中未知的缺陷(運(yùn)氣好的話這種可能性非常小)。 數(shù)據(jù)損壞而未檢測(cè)到的幾率非常之小,對(duì)于每個(gè)被處理的文件大約是四十億分之一。檢查是在解壓縮時(shí)進(jìn)行的, 因此它只能說明某個(gè)地方出問題了。它能幫助恢復(fù)原始未壓縮的數(shù)據(jù)??梢杂?bzip2recover 來嘗試從損壞的文件中恢復(fù)數(shù)據(jù)。
返回值:正常退出返回 0, 出現(xiàn)環(huán)境問題返回 1 (文件未找到,非法的選項(xiàng),I/O錯(cuò)誤等),返回 2 表明壓縮文件損壞,出現(xiàn)導(dǎo)致 bzip2 緊急退出的內(nèi)部一致性錯(cuò)誤(例如缺陷)時(shí)返回 3。
選項(xiàng)
- -c --stdout
- 將數(shù)據(jù)壓縮或解壓縮至標(biāo)準(zhǔn)輸出。
- -d --decompress
- 強(qiáng)制解壓縮。 bzip2, bunzip2 以及 bzcat 實(shí)際上是同一個(gè)程序,進(jìn)行何種操作將根據(jù)程序名確定。指定該選項(xiàng)后將不考慮這一機(jī)制,強(qiáng)制 bzip2 進(jìn)行解壓縮。
- -z --compress
- -d 選項(xiàng)的補(bǔ)充:強(qiáng)制進(jìn)行壓縮操作,而不管執(zhí)行的是哪個(gè)程序。
- -t --test
- 檢查指定文件的完整性,但并不對(duì)其解壓縮。實(shí)際上將對(duì)數(shù)據(jù)進(jìn)行實(shí)驗(yàn)性的解壓縮操作,而不輸出結(jié)果。
- -f --force
- 強(qiáng)制覆蓋輸出文件。通常 bzip2 不會(huì)覆蓋已經(jīng)存在的文件。該選項(xiàng)還強(qiáng)制 bzip2 打破文件的硬連接,缺省情況下 bzip2 不會(huì)這么做。
- -k --keep
- 在壓縮或解壓縮時(shí)保留輸入文件(不刪除這些文件)。
- -s --small
- 在壓縮、 解壓縮及檢查時(shí)減少內(nèi)存用量。 采用一種修正的算法進(jìn)行壓縮和測(cè)試,每個(gè)數(shù)據(jù)塊僅需要 2.5 個(gè)字節(jié)。這意味著任何文件都可以在 2300k 的內(nèi)存中進(jìn)行解壓縮,盡管速度只有通常情況下的一半。
在壓縮時(shí),-s將選定 200k 的塊長度,內(nèi)存用量也限制在 200k 左右,代價(jià)是壓縮率會(huì)降低。總之,如果機(jī)器的內(nèi)存較少(8兆字節(jié)或更少),可對(duì)所有操作都采用-s選項(xiàng)。參見下面的內(nèi)存管理。
- -q --quiet
- 壓制不重要的警告信息。屬于 I/O 錯(cuò)誤及其它嚴(yán)重事件的信息將不會(huì)被壓制。
- -v --verbose
- 詳盡模式 -- 顯示每個(gè)被處理文件的壓縮率。命令行中更多的 -v 選項(xiàng)將增加詳細(xì)的程度,使 bzip2 顯示出許多主要用于診斷目的信息。
- -L --license -V --version
- 顯示軟件版本,許可證條款及條件。
- -1 to -9
- 在壓縮時(shí)將塊長度設(shè)為 100 k、200 k .. 900 k。對(duì)解壓縮沒有影響。參見下面的內(nèi)存管理。
- --
- 將所有后面的命令行變量看作文件名,即使這些變量以減號(hào)"-"打頭。可用這一選項(xiàng)處理以減號(hào)"-"打頭的文件名,例如:bzip2 -- -myfilename.
- --repetitive-fast --repetitive-best
- 這些選項(xiàng)在 0.9.5 及其以上版本中是多余的。在較早的版本中,這兩個(gè)選項(xiàng)對(duì)排序算法的行為提供了一些粗糙的控制,有些情況下很有用。 0.9.5 及其以上版本采用了改進(jìn)的算法而與這些選項(xiàng)無關(guān)。
內(nèi)存管理
bzip2 按照數(shù)據(jù)塊壓縮大文件。 數(shù)據(jù)塊長度同時(shí)影響數(shù)據(jù)的壓縮率和壓縮及解壓縮時(shí)需要的內(nèi)存用量。 選項(xiàng) -1 至 -9 將數(shù)據(jù)塊長度分別指定為 100,000 字節(jié)至 900,000(缺?。┳止?jié)。在解壓縮時(shí), 壓縮時(shí)使用的塊長度從壓縮文件的頭中讀取,同時(shí) bunzip2 分配出剛好夠用的內(nèi)存對(duì)文件進(jìn)行解壓縮。由于數(shù)據(jù)塊長度保存在壓縮文件中, 所以在解壓縮時(shí)不需要 -1 至 -9 這些選項(xiàng),因而將被忽略。
可以按下面的公式估計(jì)壓縮和解壓縮時(shí)的內(nèi)存用量,單位為字節(jié):
壓縮: 400k + ( 8 x 數(shù)據(jù)塊長度 )
解壓縮: 100k + ( 4 x 數(shù)據(jù)塊長度 ), 或
100k + ( 2.5 x 數(shù)據(jù)塊長度 )
大數(shù)據(jù)塊長度產(chǎn)生迅速縮小的臨界返回 (give rapidly diminishing marginal returns)。在小機(jī)器上使用 bzip2 時(shí), 一個(gè)值得記住的事實(shí)是, 大多數(shù)壓縮來自數(shù)據(jù)塊長度的前 200 或 300k。另外重要的一點(diǎn)是, 解壓縮時(shí)內(nèi)存的需要量是在壓縮時(shí)用塊長度選項(xiàng)設(shè)定的。
對(duì)于缺省用 900k 的數(shù)據(jù)塊長度壓縮的文件, bunzip2 大約需要 3700k 字節(jié)的內(nèi)存進(jìn)行解壓縮。為支持一臺(tái) 4MB 機(jī)器上任何文件的解壓縮, bunzip2 有一個(gè)選項(xiàng)大約只需一半容量的內(nèi)存,約 2300k 字節(jié)。 解壓縮速度同樣也降低一半。因此應(yīng)該只在需要時(shí)采用該選項(xiàng)。相應(yīng)的選項(xiàng)標(biāo)志為 -s。
一般來說,應(yīng)盡量采用內(nèi)存允許的最大數(shù)據(jù)塊長度,因?yàn)檫@能達(dá)到最好的壓縮率,壓縮和解壓縮速度實(shí)質(zhì)上不受塊長度的影響。
另一個(gè)值得注意的問題是關(guān)于小于一個(gè)數(shù)據(jù)塊長度的文件的, 也就是說, 所遇到的大多數(shù)文件使用一個(gè)大數(shù)據(jù)塊。 由于文件長度小于一個(gè)數(shù)據(jù)塊長度,實(shí)際使用到的內(nèi)存與文件長度成正比。例如,采用 -9 選項(xiàng)壓縮一個(gè) 20,000 字節(jié)的文件時(shí),將分配 7600k 的內(nèi)存,但其中只用到了 400k+20000*8=560k 字節(jié)。同樣地,在解壓縮時(shí)將分配 3700k 內(nèi)存,但只用到 100k + 20000 * 4 = 180 k 字節(jié)。
下表總結(jié)了不同數(shù)據(jù)塊長度下的內(nèi)存用量。同時(shí)列出的還有 Calgary 文本壓縮語料庫中的 14 個(gè)文件的壓縮長度,這 14 個(gè)文件壓縮前總長度為 3,141,622 字節(jié)。這些數(shù)據(jù)顯示了壓縮率是如何隨數(shù)據(jù)塊長度變化的。由于這一語料庫主要由小文件組成, 所以這些數(shù)字并沒有充分體現(xiàn)出大文件情況下,采用大數(shù)據(jù)塊所能達(dá)到的較高壓縮率的優(yōu)勢(shì)。
壓縮時(shí) 解壓縮 解壓縮 -s 語料庫文件
Flag 內(nèi)存用量 內(nèi)存用量 選項(xiàng)內(nèi)存用量 壓縮長度
-1 1200k 500k 350k 914704
-2 2000k 900k 600k 877703
-3 2800k 1300k 850k 860338
-4 3600k 1700k 1100k 846899
-5 4400k 2100k 1350k 845160
-6 5200k 2500k 1600k 838626
-7 6100k 2900k 1850k 834096
-8 6800k 3300k 2100k 828642
-9 7600k 3700k 2350k 828642
從損壞的文件中恢復(fù)數(shù)據(jù)
bzip2 按數(shù)據(jù)塊對(duì)數(shù)據(jù)進(jìn)行壓縮,數(shù)據(jù)塊長度通常為 900k 字節(jié)。每個(gè)數(shù)據(jù)塊被獨(dú)立地處理。如果由于介質(zhì)或傳輸錯(cuò)誤導(dǎo)致多數(shù)據(jù)塊的 .bz2 文件損壞,有可能將文件中未損壞的數(shù)據(jù)塊中的數(shù)據(jù)恢復(fù)。
壓縮后的數(shù)據(jù)塊以一個(gè) 48 位的結(jié)構(gòu)分界,因而有可能在合理的范圍內(nèi)找到塊邊界。每個(gè)數(shù)據(jù)塊也帶著自己的 32 位 CRC 校驗(yàn)碼,因此可以區(qū)分損壞與未損壞的數(shù)據(jù)塊。
bzip2recover 是一個(gè)簡單的程序,它的功能是在 .bz2 文件中尋找數(shù)據(jù)塊,并將每個(gè)數(shù)據(jù)塊寫到自己的 .bz2 文件中。然后可以用 bzip2 -t 測(cè)試結(jié)果的完整性,將未損壞的部分解壓縮。
bzip2recover 只有一個(gè)命令行變量,即損壞文件的名字。輸出結(jié)果是一系列象 "rec0001file.bz2"、 "rec0002file.bz2" 這樣的文件,每個(gè)文件含有從損壞文件中找出的數(shù)據(jù)塊。輸出文件名設(shè)計(jì)為在接下來的處理中可方便地使用通配符,例如,"bzip2 -dc rec*file.bz2>recovered_data",可按正確的次序列出文件。
bzip2recover 在處理大文件時(shí)最有用, 因?yàn)榇笪募泻芏鄶?shù)據(jù)塊。顯然用它處理單個(gè)數(shù)據(jù)塊的損壞文件不會(huì)有任何結(jié)果,因?yàn)橐粋€(gè)損壞的數(shù)據(jù)塊是無法恢復(fù)的。如果想盡量減少潛在的由于介質(zhì)及傳輸錯(cuò)誤導(dǎo)致的數(shù)據(jù)損壞,可以考慮采用較小的數(shù)據(jù)塊長度進(jìn)行壓縮。
有關(guān)性能的注解
在壓縮的排序階段, 相似的字符串將被聚集在一起。 因此, 對(duì)于包含很長重復(fù)符號(hào)的文件, 例如象 "aabaabaabaab......" 這樣的字符串(重復(fù)幾百次), 壓縮速度會(huì)比通常情況慢得多。 0.9.5 及其以上版本在處理這樣的重復(fù)時(shí), 速度比以前版本提高了很多。 最壞情況與平均情況下的壓縮時(shí)間之比約為 10:1。 對(duì)于以前的版本,這一數(shù)字大約是 100:1 以上。你如果愿意,可采用 -vvvv 選項(xiàng)來非常詳細(xì)地監(jiān)視這一過程。
解壓縮速度并不受這些現(xiàn)象的影響。
bzip2 通常分配出幾兆字節(jié)的內(nèi)存用于處理數(shù)據(jù), 對(duì)這些內(nèi)存的訪問是以相當(dāng)隨機(jī)的方式進(jìn)行的。 這意味著, 壓縮及解壓縮的性能在很大程度上取決于機(jī)器上處理高速緩存未命中的速度。 因此,已經(jīng)觀察到對(duì)程序作很小的減少失敗率的改動(dòng)會(huì)導(dǎo)致不成比例的很大的性能上的提升。 我設(shè)想 bzip2 在有大量高速緩存機(jī)器上的性能最佳。
警告
I/O 錯(cuò)誤信息并不是很有用。 bzip2 會(huì)盡量探測(cè) I/O 錯(cuò)誤信息并干凈地退出, 但問題的細(xì)節(jié)有時(shí)看上去很容易引起誤解。
本手冊(cè)頁適用于 0.9.5 版的 bzip2。 由這一版本的 bzip2 產(chǎn)生的壓縮數(shù)據(jù)與以前的公開版本 0.1pl2、0.9.0 完全兼容,但有一個(gè)例外:0.9.0 及其以上版本能正確解壓縮多個(gè)連在一起的壓縮文件,0.1pl2 則不能, 它將在解壓縮完數(shù)據(jù)流中的第一個(gè)文件之后停止。
bzip2recover 采用 32 位的整型數(shù)表示壓縮文件中位的位置,因此它無法處理大于 512 兆字節(jié)的文件。但這一問題很容易解決。
作者
Julian Seward, jseward@acm.org.
http://www.muraroa.demon.co.uk
bzip2 包含的想法及概念至少歸功于下列人員: Michael Burrows 和 David Wheeler(塊排序變換), David Wheeler(Huffman 編碼器), Peter Fenwick(原始 bzip 的結(jié)構(gòu)編程模型及許多改進(jìn)),Alistair Moffat、 Ian Witten(原始 bzip 中的算法編碼)。我非常感激他們的幫助、 支持以及建議。 參見源發(fā)布的手冊(cè)中有關(guān)文檔來源中的線索。 Christian von Roques 曾鼓勵(lì)我尋找更快的排序算法, 以提高壓縮速度。 bela Lubkin 曾鼓勵(lì)我改進(jìn)最壞情況下的壓縮性能。 很多人給我發(fā)來修補(bǔ)程序, 幫助解決移植問題,租借機(jī)器,提出建議等。
#p#
NAME
bzip2, bunzip2 - a block-sorting file compressor, v1.0.2
bzcat - decompresses files to stdout
bzip2recover - recovers data from damaged bzip2 files
SYNOPSIS
bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ]
bunzip2 [ -fkvsVL ] [ filenames ... ]
bzcat [ -s ] [ filenames ... ]
bzip2recover filename
DESCRIPTION
bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally considerably better than that achieved by more conventional LZ77/LZ78-based compressors, and approaches the performance of the PPM family of statistical compressors.
The command-line options are deliberately very similar to those of GNU gzip, but they are not identical.
bzip2 expects a list of file names to accompany the command-line flags. Each file is replaced by a compressed version of itself, with the name "original_name.bz2". Each compressed file has the same modification date, permissions, and, when possible, ownership as the corresponding original, so that these properties can be correctly restored at decompression time. File name handling is naive in the sense that there is no mechanism for preserving original file names, permissions, ownerships or dates in filesystems which lack these concepts, or have serious file name length restrictions, such as MS-DOS.
bzip2 and bunzip2 will by default not overwrite existing files. If you want this to happen, specify the -f flag.
If no file names are specified, bzip2 compresses from standard input to standard output. In this case, bzip2 will decline to write compressed output to a terminal, as this would be entirely incomprehensible and therefore pointless.
bunzip2 (or bzip2 -d) decompresses all specified files. Files which were not created by bzip2 will be detected and ignored, and a warning issued. bzip2 attempts to guess the filename for the decompressed file from that of the compressed file as follows:
filename.bz2 becomes filename
filename.bz becomes filename
filename.tbz2 becomes filename.tar
filename.tbz becomes filename.tar
anyothername becomes anyothername.out
If the file does not end in one of the recognised endings, .bz2, .bz, .tbz2 or .tbz, bzip2 complains that it cannot guess the name of the original file, and uses the original name with .out appended.
As with compression, supplying no filenames causes decompression from standard input to standard output.
bunzip2 will correctly decompress a file which is the concatenation of two or more compressed files. The result is the concatenation of the corresponding uncompressed files. Integrity testing (-t) of concatenated compressed files is also supported.
You can also compress or decompress files to the standard output by giving the -c flag. Multiple files may be compressed and decompressed like this. The resulting outputs are fed sequentially to stdout. Compression of multiple files in this manner generates a stream containing multiple compressed file representations. Such a stream can be decompressed correctly only by bzip2 version 0.9.0 or later. Earlier versions of bzip2 will stop after decompressing the first file in the stream.
bzcat (or bzip2 -dc) decompresses all specified files to the standard output.
bzip2 will read arguments from the environment variables BZIP2 and BZIP, in that order, and will process them before any arguments read from the command line. This gives a convenient way to supply default arguments.
Compression is always performed, even if the compressed file is slightly larger than the original. Files of less than about one hundred bytes tend to get larger, since the compression mechanism has a constant overhead in the region of 50 bytes. Random data (including the output of most file compressors) is coded at about 8.05 bits per byte, giving an expansion of around 0.5%.
As a self-check for your protection, bzip2 uses 32-bit CRCs to make sure that the decompressed version of a file is identical to the original. This guards against corruption of the compressed data, and against undetected bugs in bzip2 (hopefully very unlikely). The chances of data corruption going undetected is microscopic, about one chance in four billion for each file processed. Be aware, though, that the check occurs upon decompression, so it can only tell you that something is wrong. It can't help you recover the original uncompressed data. You can use bzip2recover to try to recover data from damaged files.
Return values: 0 for a normal exit, 1 for environmental problems (file not found, invalid flags, I/O errors, &c), 2 to indicate a corrupt compressed file, 3 for an internal consistency error (eg, bug) which caused bzip2 to panic.
OPTIONS
- -c --stdout
- Compress or decompress to standard output.
- -d --decompress
- Force decompression. bzip2, bunzip2 and bzcat are really the same program, and the decision about what actions to take is done on the basis of which name is used. This flag overrides that mechanism, and forces bzip2 to decompress.
- -z --compress
- The complement to -d: forces compression, regardless of the invocation name.
- -t --test
- Check integrity of the specified file(s), but don't decompress them. This really performs a trial decompression and throws away the result.
- -f --force
- Force overwrite of output files. Normally, bzip2 will not overwrite existing output files. Also forces bzip2 to break hard links to files, which it otherwise wouldn't do.
bzip2 normally declines to decompress files which don't have the correct magic header bytes. If forced (-f), however, it will pass such files through unmodified. This is how GNU gzip behaves.
- -k --keep
- Keep (don't delete) input files during compression or decompression.
- -s --small
- Reduce memory usage, for compression, decompression and testing. Files are decompressed and tested using a modified algorithm which only requires 2.5 bytes per block byte. This means any file can be decompressed in 2300k of memory, albeit at about half the normal speed.
During compression, -s selects a block size of 200k, which limits memory use to around the same figure, at the expense of your compression ratio. In short, if your machine is low on memory (8 megabytes or less), use -s for everything. See MEMORY MANAGEMENT below.
- -q --quiet
- Suppress non-essential warning messages. Messages pertaining to I/O errors and other critical events will not be suppressed.
- -v --verbose
- Verbose mode -- show the compression ratio for each file processed. Further -v's increase the verbosity level, spewing out lots of information which is primarily of interest for diagnostic purposes.
- -L --license -V --version
- Display the software version, license terms and conditions.
- -1 (or --fast) to -9 (or --best)
- Set the block size to 100 k, 200 k .. 900 k when compressing. Has no effect when decompressing. See MEMORY MANAGEMENT below. The --fast and --best aliases are primarily for GNU gzip compatibility. In particular, --fast doesn't make things significantly faster. And --best merely selects the default behaviour.
- --
- Treats all subsequent arguments as file names, even if they start with a dash. This is so you can handle files with names beginning with a dash, for example: bzip2 -- -myfilename.
- --repetitive-fast --repetitive-best
- These flags are redundant in versions 0.9.5 and above. They provided some coarse control over the behaviour of the sorting algorithm in earlier versions, which was sometimes useful. 0.9.5 and above have an improved algorithm which renders these flags irrelevant.
MEMORY MANAGEMENT
bzip2 compresses large files in blocks. The block size affects both the compression ratio achieved, and the amount of memory needed for compression and decompression. The flags -1 through -9 specify the block size to be 100,000 bytes through 900,000 bytes (the default) respectively. At decompression time, the block size used for compression is read from the header of the compressed file, and bunzip2 then allocates itself just enough memory to decompress the file. Since block sizes are stored in compressed files, it follows that the flags -1 to -9 are irrelevant to and so ignored during decompression.
Compression and decompression requirements, in bytes, can be estimated as:
Compression: 400k + ( 8 x block size )
Decompression: 100k + ( 4 x block size ), or
100k + ( 2.5 x block size )
Larger block sizes give rapidly diminishing marginal returns. Most of the compression comes from the first two or three hundred k of block size, a fact worth bearing in mind when using bzip2 on small machines. It is also important to appreciate that the decompression memory requirement is set at compression time by the choice of block size.
For files compressed with the default 900k block size, bunzip2 will require about 3700 kbytes to decompress. To support decompression of any file on a 4 megabyte machine, bunzip2 has an option to decompress using approximately half this amount of memory, about 2300 kbytes. Decompression speed is also halved, so you should use this option only where necessary. The relevant flag is -s.
In general, try and use the largest block size memory constraints allow, since that maximises the compression achieved. Compression and decompression speed are virtually unaffected by block size.
Another significant point applies to files which fit in a single block -- that means most files you'd encounter using a large block size. The amount of real memory touched is proportional to the size of the file, since the file is smaller than a block. For example, compressing a file 20,000 bytes long with the flag -9 will cause the compressor to allocate around 7600k of memory, but only touch 400k + 20000 * 8 = 560 kbytes of it. Similarly, the decompressor will allocate 3700k but only touch 100k + 20000 * 4 = 180 kbytes.
Here is a table which summarises the maximum memory usage for different block sizes. Also recorded is the total compressed size for 14 files of the Calgary Text Compression Corpus totalling 3,141,622 bytes. This column gives some feel for how compression varies with block size. These figures tend to understate the advantage of larger block sizes for larger files, since the Corpus is dominated by smaller files.
Compress Decompress Decompress Corpus
Flag usage usage -s usage Size
-1 1200k 500k 350k 914704
-2 2000k 900k 600k 877703
-3 2800k 1300k 850k 860338
-4 3600k 1700k 1100k 846899
-5 4400k 2100k 1350k 845160
-6 5200k 2500k 1600k 838626
-7 6100k 2900k 1850k 834096
-8 6800k 3300k 2100k 828642
-9 7600k 3700k 2350k 828642
RECOVERING DATA FROM DAMAGED FILES
bzip2 compresses files in blocks, usually 900kbytes long. Each block is handled independently. If a media or transmission error causes a multi-block .bz2 file to become damaged, it may be possible to recover data from the undamaged blocks in the file.
The compressed representation of each block is delimited by a 48-bit pattern, which makes it possible to find the block boundaries with reasonable certainty. Each block also carries its own 32-bit CRC, so damaged blocks can be distinguished from undamaged ones.
bzip2recover is a simple program whose purpose is to search for blocks in .bz2 files, and write each block out into its own .bz2 file. You can then use bzip2 -t to test the integrity of the resulting files, and decompress those which are undamaged.
bzip2recover takes a single argument, the name of the damaged file, and writes a number of files "rec00001file.bz2", "rec00002file.bz2", etc, containing the extracted blocks. The output filenames are designed so that the use of wildcards in subsequent processing -- for example, "bzip2 -dc rec*file.bz2 > recovered_data" -- processes the files in the correct order.
bzip2recover should be of most use dealing with large .bz2 files, as these will contain many blocks. It is clearly futile to use it on damaged single-block files, since a damaged block cannot be recovered. If you wish to minimise any potential data loss through media or transmission errors, you might consider compressing with a smaller block size.
PERFORMANCE NOTES
The sorting phase of compression gathers together similar strings in the file. Because of this, files containing very long runs of repeated symbols, like "aabaabaabaab ..." (repeated several hundred times) may compress more slowly than normal. Versions 0.9.5 and above fare much better than previous versions in this respect. The ratio between worst-case and average-case compression time is in the region of 10:1. For previous versions, this figure was more like 100:1. You can use the -vvvv option to monitor progress in great detail, if you want.
Decompression speed is unaffected by these phenomena.
bzip2 usually allocates several megabytes of memory to operate in, and then charges all over it in a fairly random fashion. This means that performance, both for compressing and decompressing, is largely determined by the speed at which your machine can service cache misses. Because of this, small changes to the code to reduce the miss rate have been observed to give disproportionately large performance improvements. I imagine bzip2 will perform best on machines with very large caches.
CAVEATS
I/O error messages are not as helpful as they could be. bzip2 tries hard to detect I/O errors and exit cleanly, but the details of what the problem is sometimes seem rather misleading.
This manual page pertains to version 1.0.2 of bzip2. Compressed data created by this version is entirely forwards and backwards compatible with the previous public releases, versions 0.1pl2, 0.9.0, 0.9.5, 1.0.0 and 1.0.1, but with the following exception: 0.9.0 and above can correctly decompress multiple concatenated compressed files. 0.1pl2 cannot do this; it will stop after decompressing just the first file in the stream.
bzip2recover versions prior to this one, 1.0.2, used 32-bit integers to represent bit positions in compressed files, so it could not handle compressed files more than 512 megabytes long. Version 1.0.2 and above uses 64-bit ints on some platforms which support them (GNU supported targets, and Windows). To establish whether or not bzip2recover was built with such a limitation, run it without arguments. In any event you can build yourself an unlimited version if you can recompile it with MaybeUInt64 set to be an unsigned 64-bit integer.
AUTHOR
Julian Seward, jseward@acm.org.
http://sources.redhat.com/bzip2
The ideas embodied in bzip2 are due to (at least) the following people: Michael Burrows and David Wheeler (for the block sorting transformation), David Wheeler (again, for the Huffman coder), Peter Fenwick (for the structured coding model in the original bzip, and many refinements), and Alistair Moffat, Radford Neal and Ian Witten (for the arithmetic coder in the original bzip). I am much indebted for their help, support and advice. See the manual in the source distribution for pointers to sources of documentation. Christian von Roques encouraged me to look for faster sorting algorithms, so as to speed up compression. Bela Lubkin encouraged me to improve the worst-case compression performance. The bz* scripts are derived from those of GNU gzip. Many people sent patches, helped with portability problems, lent machines, gave advice and were generally helpful.