BZIP2 (.bz2)

Background & Context

    • MIME type: application/x-bzip2
    • BZIP2 compression method and file format.
    • General-purpose lossless compression method.
    • Occasionally used for archiving data on Unix systems and for exchanging files on the internet.
    • Binary format.
    • Compresses data using a combination of the BurrowsWheeler transform and Huffman entropy coding.
    • Stores a single file.
    • Typically used in combination with TAR.
    • Developed and maintained by Julian Seward.
    • Introduced in 1996, released as Version 1.0 in 2000.

Import & Export

  • Import["file.bz2"] uncompresses file.bz2 and imports file, automatically inferring the format from the file contents.
  • Import["file.ext.bz2"] uncompresses and imports a BZIP2-compressed file.
  • Import["file.ext.bz2",elem] imports the specified element from file.ext file.
  • Import["file.bz2","format"] uncompresses a BZIP2 file and imports it as "format".
  • Import["file.ext.bz2",{elem,suba,subb,}] imports a subelement.
  • Import["file.ext.bz2",{{elem1,elem2,}}] imports multiple elements.
  • The import format can be explicitly specified with Import["file.bz2",{"format",elems1,elems2,}].
  • Import["file","BZIP2"] or Import["file",{"BZIP2",elem,}] imports any BZIP2 file.
  • Export["file.ext.bz2",expr] exports any expression expr to a BZIP2 compressed file whose format is implied by the file name extension .ext.
  • Export["file.ext.bz2",expr,elem] creates a BZIP2 compressed .ext file by treating expr as specifying element elem.
  • Export["file.ext.bz2",expr,opt1->val1,] exports expr with the specified option elements taken to have the specified values.
  • Export["file.ext.bz2",{expr1,expr2,},{{elem1,elem2,}}] treats each expri as specifying the corresponding elemi.
  • The export format and BZIP2 compression method can be explicitly specified with Export["file",exprs,{"BZIP2","format",elems}].
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • The BZIP2 format itself does not support Import or Export elements.
  • Import elements of the BZIP2-compressed file:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Import["file.ext.bz2","Elements"] returns the names of elements available in file.ext, effectively unpacking the compressed file before importing "file.ext".

Examples

open allclose all

Basic Examples  (2)

Export a string to a BZIP2-compressed text file:

Convert the previous output back to a Wolfram Language string:

Export graphics to a BZIP2-compressed PNG file:

This gives the available Import elements of the PNG file:

When importing any element from a BZIP2 file, the Wolfram Language automatically uncompresses it:

Properties & Relations  (2)

Get some text to export:

Compare GZIP, BZIP2 and ZSTD compression to the uncompressed size:

Compare GZIP, BZIP2 and ZSTD compression speeds:

The amount of compression depends on the data being compressed. For example, JPEG images are already compressed and cannot be compressed any further by GZIP: