bzip2

Updated: 07/31/2022 by Computer Hope
bzip2 logo

Originally created by Julian Seward, bzip2 is a free, open source data compression utility. It is useful for reducing file sizes so more files can be stored or files can be transferred quickly. Also, it helps files recover from some media errors (bad disk). The bzip2 utility is patent free and available to commercially. It uses the Burrows-Wheeler algorithm, which replaces sets of duplicate characters and is currently maintained by Mark Wielaard and Micah Snyder.

When files are compressed with bzip2, they gain the .bz2 file extension.

How to install bzip2

Instructions for installing bzip2 are located in the README file that comes with the installation files. It compiles like C and C++ files using an ANSI C compiler. In the example below, bzip2 is installed to /Users/ch/bzip2-1.0.8 instead of the default directories.

[email protected] ~/bzip2-1.0.8 : make install PREFIX=/Users/ch/bzip2-1.0.8
gcc -Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64 -c bzip2recover.c
gcc -Wall -Winline -O2 -g -D_FILE_OFFSET_BITS=64  -o bzip2recover bzip2recover.o
if ( test ! -d /Users/ch/bzip2-1.0.8/bin ) ; then mkdir -p /Users/ch/bzip2-1.0.8/bin ; fi
if ( test ! -d /Users/ch/bzip2-1.0.8/lib ) ; then mkdir -p /Users/ch/bzip2-1.0.8/lib ; fi
if ( test ! -d /Users/ch/bzip2-1.0.8/man ) ; then mkdir -p /Users/ch/bzip2-1.0.8/man ; fi
if ( test ! -d /Users/ch/bzip2-1.0.8/man/man1 ) ; then mkdir -p /Users/ch/bzip2-1.0.8/man/man1 ; fi
if ( test ! -d /Users/ch/bzip2-1.0.8/include ) ; then mkdir -p /Users/ch/bzip2-1.0.8/include ; fi
cp -f bzip2 /Users/ch/bzip2-1.0.8/bin/bzip2
cp -f bzip2 /Users/ch/bzip2-1.0.8/bin/bunzip2
cp -f bzip2 /Users/ch/bzip2-1.0.8/bin/bzcat
cp -f bzip2recover /Users/ch/bzip2-1.0.8/bin/bzip2recover
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzip2
chmod a+x /Users/ch/bzip2-1.0.8/bin/bunzip2
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzcat
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzip2recover
cp -f bzip2.1 /Users/ch/bzip2-1.0.8/man/man1
chmod a+r /Users/ch/bzip2-1.0.8/man/man1/bzip2.1
cp -f bzlib.h /Users/ch/bzip2-1.0.8/include
chmod a+r /Users/ch/bzip2-1.0.8/include/bzlib.h
cp -f libbz2.a /Users/ch/bzip2-1.0.8/lib
chmod a+r /Users/ch/bzip2-1.0.8/lib/libbz2.a
cp -f bzgrep /Users/ch/bzip2-1.0.8/bin/bzgrep
ln -s -f /Users/ch/bzip2-1.0.8/bin/bzgrep /Users/ch/bzip2-1.0.8/bin/bzegrep
ln -s -f /Users/ch/bzip2-1.0.8/bin/bzgrep /Users/ch/bzip2-1.0.8/bin/bzfgrep
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzgrep
cp -f bzmore /Users/ch/bzip2-1.0.8/bin/bzmore
ln -s -f /Users/ch/bzip2-1.0.8/bin/bzmore /Users/ch/bzip2-1.0.8/bin/bzless
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzmore
cp -f bzdiff /Users/ch/bzip2-1.0.8/bin/bzdiff
ln -s -f /Users/ch/bzip2-1.0.8/bin/bzdiff /Users/ch/bzip2-1.0.8/bin/bzcmp
chmod a+x /Users/ch/bzip2-1.0.8/bin/bzdiff
cp -f bzgrep.1 bzmore.1 bzdiff.1 /Users/ch/bzip2-1.0.8/man/man1
chmod a+r /Users/ch/bzip2-1.0.8/man/man1/bzgrep.1
chmod a+r /Users/ch/bzip2-1.0.8/man/man1/bzmore.1
chmod a+r /Users/ch/bzip2-1.0.8/man/man1/bzdiff.1
echo ".so man1/bzgrep.1" > /Users/ch/bzip2-1.0.8/man/man1/bzegrep.1
echo ".so man1/bzgrep.1" > /Users/ch/bzip2-1.0.8/man/man1/bzfgrep.1
echo ".so man1/bzmore.1" > /Users/ch/bzip2-1.0.8/man/man1/bzless.1
echo ".so man1/bzdiff.1" > /Users/ch/bzip2-1.0.8/man/man1/bzcmp.1
[email protected] ~/bzip2-1.0.8 

If you see many warnings or compile errors, you must fix them. You can search the Internet for the specific error and make the necessary changes. For example, on a Mac M1 Max, %L is not supported and needs to be changed to %ll. Using %L results in the following warning.

bzip2 example mac install error

To fix this, change bzip2recover.c, replacing define MaybeUInt64_FMT "%Lu" with define MaybeUInt64_FMT "%llu".

How to compress files using bzip2

To compress files, use the command line or terminal and change to the directory with the files you want to compress. Run bzip2 -z [files].

[email protected] ~ : cd tmp
[email protected] ~/tmp : ls
[email protected] 6.1k ch 16 Jul 08:21 .DS_Store
[email protected] 282k ch  8 May 11:44 bowl_floral.jpg
[email protected] 261k ch  8 May 11:55 bowl_floral_final.jpg
[email protected] 9.9M ch 23 Apr 14:52 bubble_flower.jpg
[email protected] 8.1M ch 23 Apr 14:52 bubble_flower_final.jpg
[email protected] 1.1M ch 22 Mar 16:39 canvas_test.jpg
[email protected] 1.2M ch 22 Mar 16:39 canvas_test_final.jpg
[email protected] 430k ch  8 May 20:17 cat_in_window.jpg
[email protected] 779k ch 10 Jun 21:08 flame_thing.jpg
[email protected] 747k ch 10 Jun 21:13 flame_thing_final.jpg
[email protected] 2.5M ch  9 Jun 13:05 floral_20220609.jpg
[email protected] 2.1M ch  9 Jun 13:05 floral_20220609_final.jpg
[email protected] 5.0M ch 11 Jun 20:20 img_4167_custom_final.jpg
[email protected] 2.1M ch 16 May 19:43 No_Fill.jpg
.rw-r--r--  2.4M ch 16 May 19:58 no_fill_final.jpg
.rw-r--r--  533k ch 19 Mar 17:12 quick_study_final.jpg
[email protected] 314k ch 16 Apr 15:03 replace_household_items.jpg
[email protected] 371k ch  8 May 07:43 stone_floral.jpg
[email protected] 411k ch  8 May 07:44 stone_floral_final.jpg
[email protected] ~/tmp : du
75472	.
[email protected] ~/tmp : bzip2 -z *
[email protected] ~/tmp : ls
[email protected] 6.1k ch 16 Jul 08:21 .DS_Store
.rw-r--r--  271k ch  8 May 11:44 bowl_floral.jpg.bz2
[email protected] 242k ch  8 May 11:55 bowl_floral_final.jpg.bz2
.rw-r--r--  9.9M ch 23 Apr 14:52 bubble_flower.jpg.bz2
[email protected] 7.9M ch 23 Apr 14:52 bubble_flower_final.jpg.bz2
.rw-r--r--  1.1M ch 22 Mar 16:39 canvas_test.jpg.bz2
[email protected] 1.2M ch 22 Mar 16:39 canvas_test_final.jpg.bz2
[email protected] 297k ch  8 May 20:17 cat_in_window.jpg.bz2
.rw-r--r--  759k ch 10 Jun 21:08 flame_thing.jpg.bz2
[email protected] 728k ch 10 Jun 21:13 flame_thing_final.jpg.bz2
.rw-r--r--  2.5M ch  9 Jun 13:05 floral_20220609.jpg.bz2
[email protected] 2.0M ch  9 Jun 13:05 floral_20220609_final.jpg.bz2
[email protected] 4.9M ch 11 Jun 20:20 img_4167_custom_final.jpg.bz2
.rw-r--r--  1.8M ch 16 May 19:43 No_Fill.jpg.bz2
.rw-r--r--  2.0M ch 16 May 19:58 no_fill_final.jpg.bz2
.rw-r--r--  509k ch 19 Mar 17:12 quick_study_final.jpg.bz2
[email protected] 271k ch 16 Apr 15:03 replace_household_items.jpg.bz2
[email protected] 351k ch  8 May 07:43 stone_floral.jpg.bz2
[email protected] 394k ch  8 May 07:44 stone_floral_final.jpg.bz2
[email protected] ~/tmp : du
72456	.
[email protected] ~/tmp : 

How to uncompress files using bzip2

To uncompress files, use the command line or terminal and change to the directory with the files you want to uncompress. Run bunzip2 [files].

[email protected] ~/tmp : ls
[email protected] 6.1k ch 16 Jul 08:21 .DS_Store
.rw-r--r--  271k ch  8 May 11:44 bowl_floral.jpg.bz2
[email protected] 242k ch  8 May 11:55 bowl_floral_final.jpg.bz2
.rw-r--r--  9.9M ch 23 Apr 14:52 bubble_flower.jpg.bz2
[email protected] 7.9M ch 23 Apr 14:52 bubble_flower_final.jpg.bz2
.rw-r--r--  1.1M ch 22 Mar 16:39 canvas_test.jpg.bz2
[email protected] 1.2M ch 22 Mar 16:39 canvas_test_final.jpg.bz2
[email protected] 297k ch  8 May 20:17 cat_in_window.jpg.bz2
.rw-r--r--  759k ch 10 Jun 21:08 flame_thing.jpg.bz2
[email protected] 728k ch 10 Jun 21:13 flame_thing_final.jpg.bz2
.rw-r--r--  2.5M ch  9 Jun 13:05 floral_20220609.jpg.bz2
[email protected] 2.0M ch  9 Jun 13:05 floral_20220609_final.jpg.bz2
[email protected] 4.9M ch 11 Jun 20:20 img_4167_custom_final.jpg.bz2
.rw-r--r--  1.8M ch 16 May 19:43 No_Fill.jpg.bz2
.rw-r--r--  2.0M ch 16 May 19:58 no_fill_final.jpg.bz2
.rw-r--r--  509k ch 19 Mar 17:12 quick_study_final.jpg.bz2
[email protected] 271k ch 16 Apr 15:03 replace_household_items.jpg.bz2
[email protected] 351k ch  8 May 07:43 stone_floral.jpg.bz2
[email protected] 394k ch  8 May 07:44 stone_floral_final.jpg.bz2
[email protected] ~/tmp : du
72456	.
[email protected] ~/tmp : bunzip2 *
[email protected] ~/tmp : ls
[email protected] 6.1k ch 16 Jul 08:21 .DS_Store
.rw-r--r--  282k ch  8 May 11:44 bowl_floral.jpg
[email protected] 261k ch  8 May 11:55 bowl_floral_final.jpg
.rw-r--r--  9.9M ch 23 Apr 14:52 bubble_flower.jpg
[email protected] 8.1M ch 23 Apr 14:52 bubble_flower_final.jpg
.rw-r--r--  1.1M ch 22 Mar 16:39 canvas_test.jpg
[email protected] 1.2M ch 22 Mar 16:39 canvas_test_final.jpg
[email protected] 430k ch  8 May 20:17 cat_in_window.jpg
.rw-r--r--  779k ch 10 Jun 21:08 flame_thing.jpg
[email protected] 747k ch 10 Jun 21:13 flame_thing_final.jpg
.rw-r--r--  2.5M ch  9 Jun 13:05 floral_20220609.jpg
[email protected] 2.1M ch  9 Jun 13:05 floral_20220609_final.jpg
[email protected] 5.0M ch 11 Jun 20:20 img_4167_custom_final.jpg
.rw-r--r--  2.1M ch 16 May 19:43 No_Fill.jpg
.rw-r--r--  2.4M ch 16 May 19:58 no_fill_final.jpg
.rw-r--r--  533k ch 19 Mar 17:12 quick_study_final.jpg
[email protected] 314k ch 16 Apr 15:03 replace_household_items.jpg
[email protected] 371k ch  8 May 07:43 stone_floral.jpg
[email protected] 411k ch  8 May 07:44 stone_floral_final.jpg
[email protected] ~/tmp : du
75472	.
[email protected] ~/tmp : 

Using bzip2 with tar

To compress or expand a tar archive named archive.tar.bz2 that was compressed with bzip2, specify the -j or --bzip2 option. (Note that tar lets you specify the short form of options without the dash.)

For example, to extract files from a tar archive that was compressed with bzip2, you can use the following command:

tar --extract --bzip2 --file archive.tar.bz2

Or, using the short form of those options:

tar xjf archive.tar.bz2

To create a tar archive of the folder hope in the current directory, and compress it with bzip2:

tar --create --bzip2 --file hope.tar.bz2 hope/

Or, using short options:

tar cjf hope.tar.bz2 hope/
Tip

On some versions of tar, such as the default version installed on macOS, bzip compression is automatically detected when extracting files from an archive. On those versions of tar, the -j / --bzip2 option is not needed when using the -x / --extract option.

Compressed file, Compression utility, Crunch, Data, Explode, Extract, Gzip, Linux cc command, Operating system terms, Pack, RAR, Stuff, Uncompress, Zip