<s>
bzip2	B-Application
is	O
a	O
free	B-License
and	I-License
open-source	I-License
file	B-General_Concept
compression	I-General_Concept
program	O
that	O
uses	O
the	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
It	O
only	O
compresses	O
single	O
files	O
and	O
is	O
not	O
a	O
file	B-Application
archiver	I-Application
.	O
</s>
<s>
bzip2	B-Application
was	O
initially	O
released	O
in	O
1996	O
by	O
Julian	O
Seward	O
.	O
</s>
<s>
It	O
compresses	O
most	O
files	O
more	O
effectively	O
than	O
older	O
LZW	B-Algorithm
and	O
Deflate	B-Algorithm
compression	I-Algorithm
algorithms	I-Algorithm
but	O
is	O
slower	O
.	O
</s>
<s>
bzip2	B-Application
is	O
particularly	O
efficient	O
for	O
text	O
data	O
,	O
and	O
decompression	O
is	O
relatively	O
fast	O
.	O
</s>
<s>
The	O
algorithm	O
uses	O
several	O
layers	O
of	O
compression	B-General_Concept
techniques	I-General_Concept
,	O
such	O
as	O
run-length	B-Algorithm
encoding	I-Algorithm
(	O
RLE	O
)	O
,	O
Burrows-Wheeler	B-Algorithm
transform	I-Algorithm
(	O
BWT	O
)	O
,	O
move-to-front	B-Algorithm
(	O
MTF	B-Algorithm
)	O
transform	O
,	O
and	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
bzip2	B-Application
compresses	O
data	O
in	O
blocks	O
between	O
100	O
and	O
900	O
kB	O
and	O
uses	O
the	O
Burrows-Wheeler	B-Algorithm
transform	I-Algorithm
to	O
convert	O
frequently	O
recurring	O
character	O
sequences	O
into	O
strings	O
of	O
identical	O
letters	O
.	O
</s>
<s>
The	O
move-to-front	B-Algorithm
transform	I-Algorithm
and	O
Huffman	B-General_Concept
coding	I-General_Concept
are	O
then	O
applied	O
.	O
</s>
<s>
There	O
have	O
been	O
some	O
modifications	O
to	O
the	O
algorithm	O
,	O
such	O
as	O
pbzip2	O
,	O
which	O
uses	O
multi-threading	B-Operating_System
to	O
improve	O
compression	O
speed	O
on	O
multi-CPU	O
and	O
multi-core	O
computers	O
.	O
</s>
<s>
bzip2	B-Application
is	O
suitable	O
for	O
use	O
in	O
big	B-Application
data	I-Application
applications	O
with	O
cluster	O
computing	O
frameworks	O
like	O
Hadoop	B-Application
and	O
Apache	B-Language
Spark	I-Language
,	O
as	O
the	O
compressed	O
blocks	O
can	O
be	O
independently	O
decompressed	O
.	O
</s>
<s>
Seward	O
made	O
the	O
first	O
public	O
release	O
of	O
bzip2	B-Application
,	O
version	O
0.15	O
,	O
in	O
July	O
1996	O
.	O
</s>
<s>
Following	O
a	O
nine-year	O
hiatus	O
of	O
updates	O
for	O
the	O
project	O
since	O
2010	O
,	O
on	O
4	O
June	O
2019	O
Federico	O
Mena	O
accepted	O
maintainership	O
of	O
the	O
bzip2	B-Application
project	O
.	O
</s>
<s>
Bzip2	B-Application
uses	O
several	O
layers	O
of	O
compression	B-General_Concept
techniques	I-General_Concept
stacked	O
on	O
top	O
of	O
each	O
other	O
,	O
which	O
occur	O
in	O
the	O
following	O
order	O
during	O
compression	O
and	O
the	O
reverse	O
order	O
during	O
decompression	O
:	O
</s>
<s>
Run-length	B-Algorithm
encoding	I-Algorithm
(	O
RLE	O
)	O
of	O
initial	O
data	O
.	O
</s>
<s>
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
(	O
BWT	O
)	O
,	O
or	O
block	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
Move-to-front	B-Algorithm
(	O
MTF	B-Algorithm
)	O
transform	O
.	O
</s>
<s>
Run-length	B-Algorithm
encoding	I-Algorithm
(	O
RLE	O
)	O
of	O
MTF	B-Algorithm
result	O
.	O
</s>
<s>
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
Delta	B-Algorithm
encoding	I-Algorithm
( Δ	O
)	O
of	O
Huffman-code	O
bit	O
lengths	O
.	O
</s>
<s>
Sparse	O
bit	B-Data_Structure
array	I-Data_Structure
showing	O
which	O
symbols	O
are	O
used	O
.	O
</s>
<s>
Runs	O
of	O
symbols	O
are	O
always	O
transformed	O
after	O
4	O
consecutive	O
symbols	O
,	O
even	O
if	O
the	O
run-length	B-Algorithm
is	O
set	O
to	O
zero	O
,	O
to	O
keep	O
the	O
transformation	O
reversible	O
.	O
</s>
<s>
The	O
author	O
of	O
bzip2	B-Application
has	O
stated	O
that	O
the	O
RLE	O
step	O
was	O
a	O
historical	O
mistake	O
and	O
was	O
only	O
intended	O
to	O
protect	O
the	O
original	O
BWT	O
implementation	O
from	O
pathological	O
cases	O
.	O
</s>
<s>
The	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
is	O
the	O
reversible	O
block-sort	O
that	O
is	O
at	O
the	O
core	O
of	O
bzip2	B-Application
.	O
</s>
<s>
The	O
block	O
is	O
entirely	O
self-contained	O
,	O
with	O
input	O
and	O
output	O
buffers	O
remaining	O
of	O
the	O
same	O
size	O
—	O
in	O
bzip2	B-Application
,	O
the	O
operating	O
limit	O
for	O
this	O
stage	O
is	O
For	O
the	O
block-sort	O
,	O
a	O
(	O
notional	O
)	O
matrix	O
is	O
created	O
,	O
in	O
which	O
row	O
i	O
contains	O
the	O
whole	O
of	O
the	O
buffer	O
,	O
rotated	O
to	O
start	O
from	O
the	O
i-th	O
symbol	O
.	O
</s>
<s>
The	O
move-to-front	B-Algorithm
transform	I-Algorithm
again	O
does	O
not	O
alter	O
the	O
size	O
of	O
the	O
processed	O
block	O
.	O
</s>
<s>
As	O
the	O
MTF	B-Algorithm
transform	I-Algorithm
assigns	O
low	O
values	O
to	O
symbols	O
that	O
reappear	O
frequently	O
,	O
this	O
results	O
in	O
a	O
data	O
stream	O
containing	O
many	O
symbols	O
in	O
the	O
low	O
integer	O
range	O
,	O
many	O
of	O
them	O
being	O
identical	O
(	O
different	O
recurring	O
input	O
symbols	O
can	O
actually	O
map	O
to	O
the	O
same	O
output	O
symbol	O
)	O
.	O
</s>
<s>
Long	O
strings	O
of	O
zeros	O
in	O
the	O
output	O
of	O
the	O
move-to-front	B-Algorithm
transform	I-Algorithm
(	O
which	O
come	O
from	O
repeated	O
symbols	O
in	O
the	O
output	O
of	O
the	O
BWT	O
)	O
are	O
replaced	O
by	O
a	O
sequence	O
of	O
two	O
special	O
codes	O
,	O
RUNA	O
and	O
RUNB	O
,	O
which	O
represent	O
the	O
run-length	B-Algorithm
as	O
a	O
binary	O
number	O
.	O
</s>
<s>
(	O
This	O
step	O
in	O
fact	O
is	O
done	O
at	O
the	O
same	O
time	O
as	O
MTF	B-Algorithm
is	O
;	O
whenever	O
MTF	B-Algorithm
would	O
produce	O
zero	O
,	O
it	O
instead	O
increases	O
a	O
counter	O
to	O
then	O
encode	O
with	O
RUNA	O
and	O
RUNB	O
.	O
)	O
</s>
<s>
The	O
run-length	B-Algorithm
code	O
is	O
terminated	O
by	O
reaching	O
another	O
normal	O
symbol	O
.	O
</s>
<s>
The	O
run-length	B-Algorithm
is	O
encoded	O
in	O
this	O
fashion	O
:	O
assigning	O
place	O
values	O
of	O
1	O
to	O
the	O
first	O
bit	O
,	O
2	O
to	O
the	O
second	O
,	O
4	O
to	O
the	O
third	O
,	O
etc	O
.	O
</s>
<s>
If	O
there	O
are	O
n	O
different	O
bytes	O
(	O
symbols	O
)	O
used	O
in	O
the	O
uncompressed	B-General_Concept
data	O
,	O
then	O
the	O
Huffman	B-General_Concept
code	I-General_Concept
will	O
consist	O
of	O
two	O
RLE	O
codes	O
(	O
RUNA	O
and	O
RUNB	O
)	O
,	O
n	O
−	O
1	O
symbol	O
codes	O
and	O
one	O
end-of-stream	O
code	O
.	O
</s>
<s>
Because	O
of	O
the	O
combined	O
result	O
of	O
the	O
MTF	B-Algorithm
and	O
RLE	O
encodings	O
in	O
the	O
previous	O
two	O
steps	O
,	O
there	O
is	O
never	O
any	O
need	O
to	O
explicitly	O
reference	O
the	O
first	O
symbol	O
in	O
the	O
MTF	B-Algorithm
table	O
(	O
would	O
be	O
zero	O
in	O
the	O
ordinary	O
MTF	B-Algorithm
)	O
,	O
thus	O
saving	O
one	O
symbol	O
for	O
the	O
end-of-stream	O
marker	O
(	O
and	O
explaining	O
why	O
only	O
n	O
−	O
1	O
symbols	O
are	O
coded	O
in	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
)	O
.	O
</s>
<s>
In	O
the	O
extreme	O
case	O
where	O
only	O
one	O
symbol	O
is	O
used	O
in	O
the	O
uncompressed	B-General_Concept
data	O
,	O
there	O
will	O
be	O
no	O
symbol	O
codes	O
at	O
all	O
in	O
the	O
Huffman	B-General_Concept
tree	I-General_Concept
,	O
and	O
the	O
entire	O
block	O
will	O
consist	O
of	O
RUNA	O
and	O
RUNB	O
(	O
implicitly	O
repeating	O
the	O
single	O
byte	O
)	O
and	O
an	O
end-of-stream	O
marker	O
with	O
value	O
2	O
.	O
</s>
<s>
This	O
has	O
the	O
advantage	O
of	O
having	O
very	O
responsive	O
Huffman	O
dynamics	O
without	O
having	O
to	O
continuously	O
supply	O
new	O
tables	O
,	O
as	O
would	O
be	O
required	O
in	O
DEFLATE	B-Algorithm
.	O
</s>
<s>
Run-length	B-Algorithm
encoding	I-Algorithm
in	O
the	O
previous	O
step	O
is	O
designed	O
to	O
take	O
care	O
of	O
codes	O
that	O
have	O
an	O
inverse	O
probability	O
of	O
use	O
higher	O
than	O
the	O
shortest	O
code	O
Huffman	B-General_Concept
code	I-General_Concept
in	O
use	O
.	O
</s>
<s>
The	O
selection	O
is	O
into	O
a	O
MTF	B-Algorithm
list	O
of	O
the	O
tables	O
.	O
</s>
<s>
Rather	O
than	O
unary	O
encoding	O
,	O
effectively	O
this	O
is	O
an	O
extreme	O
form	O
of	O
a	O
Huffman	B-General_Concept
tree	I-General_Concept
,	O
where	O
each	O
code	O
has	O
half	O
the	O
probability	O
of	O
the	O
previous	O
code	O
.	O
</s>
<s>
Huffman-code	O
bit	O
lengths	O
are	O
required	O
to	O
reconstruct	O
each	O
of	O
the	O
used	O
canonical	B-Algorithm
Huffman	I-Algorithm
tables	I-Algorithm
.	O
</s>
<s>
As	O
a	O
result	O
of	O
the	O
earlier	O
MTF	B-Algorithm
encoding	O
,	O
code	O
lengths	O
would	O
start	O
at	O
2	O
–	O
3	O
bits	O
long	O
(	O
very	O
frequently	O
used	O
codes	O
)	O
and	O
gradually	O
increase	O
,	O
meaning	O
that	O
the	O
delta	O
format	O
is	O
fairly	O
efficient	O
,	O
requiring	O
around	O
300	O
bits	O
(	O
38	O
bytes	O
)	O
per	O
full	O
Huffman	O
table	O
.	O
</s>
<s>
A	O
bitmap	O
is	O
used	O
to	O
show	O
which	O
symbols	O
are	O
used	O
inside	O
the	O
block	O
and	O
should	O
be	O
included	O
in	O
the	O
Huffman	B-General_Concept
trees	I-General_Concept
.	O
</s>
<s>
Binary	O
data	O
is	O
likely	O
to	O
use	O
all	O
256	O
symbols	O
representable	O
by	O
a	O
byte	O
,	O
whereas	O
textual	O
data	O
may	O
only	O
use	O
a	O
small	O
subset	O
of	O
available	O
values	O
,	O
perhaps	O
covering	O
the	O
ASCII	B-Protocol
range	O
between	O
32	O
and	O
126	O
.	O
</s>
<s>
The	O
presence	O
of	O
each	O
of	O
these	O
16	O
ranges	O
is	O
indicated	O
by	O
an	O
additional	O
16-bit	O
bit	B-Data_Structure
array	I-Data_Structure
at	O
the	O
front	O
.	O
</s>
<s>
For	O
contrast	O
,	O
the	O
DEFLATE	B-Algorithm
algorithm	O
would	O
show	O
the	O
absence	O
of	O
symbols	O
by	O
encoding	O
the	O
symbols	O
as	O
having	O
a	O
zero	O
bit	O
length	O
with	O
run-length	B-Algorithm
encoding	I-Algorithm
and	O
additional	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
No	O
formal	O
specification	O
for	O
bzip2	B-Application
exists	O
,	O
although	O
an	O
informal	O
specification	O
has	O
been	O
reverse	O
engineered	O
from	O
the	O
reference	O
implementation	O
.	O
</s>
<s>
As	O
an	O
overview	O
,	O
a	O
.bz2	B-Application
stream	O
consists	O
of	O
a	O
4-byte	O
header	O
,	O
followed	O
by	O
zero	O
or	O
more	O
compressed	O
blocks	O
,	O
immediately	O
followed	O
by	O
an	O
end-of-stream	O
marker	O
containing	O
a	O
32-bit	O
CRC	O
for	O
the	O
plaintext	O
whole	O
stream	O
processed	O
.	O
</s>
<s>
Because	O
of	O
the	O
first-stage	O
RLE	B-Algorithm
compression	I-Algorithm
(	O
see	O
above	O
)	O
,	O
the	O
maximum	O
length	O
of	O
plaintext	O
that	O
a	O
single	O
900kB	O
bzip2	B-Application
block	O
can	O
contain	O
is	O
around	O
46MB	O
(	O
45,899,236	O
bytes	O
)	O
.	O
</s>
<s>
This	O
can	O
occur	O
if	O
the	O
whole	O
plaintext	O
consists	O
entirely	O
of	O
repeated	O
values	O
(	O
the	O
resulting	O
.bz2	B-Application
file	B-Operating_System
in	O
this	O
case	O
is	O
46bytes	O
long	O
)	O
.	O
</s>
<s>
An	O
even	O
smaller	O
file	B-Operating_System
of	O
40bytes	O
can	O
be	O
achieved	O
by	O
using	O
an	O
input	O
containing	O
entirely	O
values	O
of	O
251	O
,	O
an	O
apparent	O
compression	O
ratio	O
of	O
1147480.9:1	O
.	O
</s>
<s>
The	O
compressed	O
blocks	O
in	O
bzip2	B-Application
can	O
be	O
independently	O
decompressed	O
,	O
without	O
having	O
to	O
process	O
earlier	O
blocks	O
.	O
</s>
<s>
This	O
means	O
that	O
bzip2	B-Application
files	O
can	O
be	O
decompressed	O
in	O
parallel	O
,	O
making	O
it	O
a	O
good	O
format	O
for	O
use	O
in	O
big	B-Application
data	I-Application
applications	O
with	O
cluster	O
computing	O
frameworks	O
like	O
Hadoop	B-Application
and	O
Apache	B-Language
Spark	I-Language
.	O
</s>
<s>
bzip2	B-Application
compresses	O
most	O
files	O
more	O
effectively	O
than	O
the	O
older	O
LZW	B-Algorithm
(	O
.Z	B-Device
)	O
and	O
Deflate	B-Algorithm
(	O
.zip	B-General_Concept
and	O
.gz	B-Application
)	O
compression	B-General_Concept
algorithms	I-General_Concept
,	O
but	O
is	O
considerably	O
slower	O
.	O
</s>
<s>
LZMA	B-General_Concept
is	O
generally	O
more	O
space-efficient	O
than	O
bzip2	B-Application
at	O
the	O
expense	O
of	O
even	O
slower	O
compression	O
speed	O
,	O
while	O
having	O
much	O
faster	O
decompression	O
.	O
</s>
<s>
bzip2	B-Application
compresses	O
data	O
in	O
blocks	O
of	O
size	O
between	O
100	O
and	O
900	O
kB	O
and	O
uses	O
the	O
Burrows	B-Algorithm
–	I-Algorithm
Wheeler	I-Algorithm
transform	I-Algorithm
to	O
convert	O
frequently-recurring	O
character	O
sequences	O
into	O
strings	O
of	O
identical	O
letters	O
.	O
</s>
<s>
It	O
then	O
applies	O
move-to-front	B-Algorithm
transform	I-Algorithm
and	O
Huffman	B-General_Concept
coding	I-General_Concept
.	O
</s>
<s>
bzip2	B-Application
's	O
ancestor	O
bzip	B-Application
used	O
arithmetic	B-Algorithm
coding	I-Algorithm
instead	O
of	O
Huffman	O
.	O
</s>
<s>
bzip3	O
,	O
a	O
modern	O
compressor	O
that	O
shares	O
common	O
ancestry	O
and	O
set	O
of	O
algorithms	O
with	O
bzip2	B-Application
,	O
switched	O
back	O
to	O
arithmetic	B-Algorithm
coding	I-Algorithm
.	O
</s>
<s>
bzip2	B-Application
performance	O
is	O
asymmetric	O
,	O
as	O
decompression	O
is	O
relatively	O
fast	O
.	O
</s>
<s>
Motivated	O
by	O
the	O
long	O
time	O
required	O
for	O
compression	O
,	O
a	O
modified	O
version	O
was	O
created	O
in	O
2003	O
called	O
pbzip2	O
that	O
used	O
multi-threading	B-Operating_System
to	O
encode	O
the	O
file	B-Operating_System
in	O
multiple	O
chunks	O
,	O
giving	O
almost	O
linear	O
speedup	O
on	O
multi-CPU	O
and	O
multi-core	O
computers	O
.	O
</s>
<s>
Like	O
gzip	B-Application
,	O
bzip2	B-Application
is	O
only	O
a	O
data	O
compressor	O
.	O
</s>
<s>
It	O
is	O
not	O
an	O
archiver	O
like	O
tar	B-Application
or	O
ZIP	B-General_Concept
;	O
the	O
program	O
itself	O
has	O
no	O
facilities	O
for	O
multiple	O
files	O
,	O
encryption	O
or	O
archive-splitting	O
,	O
but	O
,	O
in	O
the	O
UNIX	B-Operating_System
tradition	I-Operating_System
,	O
relies	O
instead	O
on	O
separate	O
external	O
utilities	O
such	O
as	O
tar	B-Application
and	O
GnuPG	B-Application
for	O
these	O
tasks	O
.	O
</s>
<s>
The	O
grep-based	O
bzgrep	O
tool	O
allows	O
directly	O
searching	O
through	O
compressed	O
text	O
without	O
needing	O
to	O
uncompress	B-Device
the	O
contents	O
first	O
.	O
</s>
