GZIP (.gz)

  • Import and Export support the GZIP file format specification Version 4.3.
  • Background

      MIME type: application/x-gzip
      GZIP compression method and file format.
      General-purpose lossless compression method.
      Commonly used for archiving data and for exchanging files on the internet.
      Often used in combination with TAR.
      Popular on Unix systems.
      The GZIP file format is described in the internet standard recommendation RFC 1952.
      Binary format.
      Stores a single file.

    Import and Export

    • Import["file.ext.gz"] uncompresses and imports a GZIP-compressed archive.
    • Export["file.ext.gz",expr] exports any expression expr to a GZIP-compressed file whose format is implied by the file name extension .ext.
    • Import["file.gz"] uncompresses file.gz and imports file, automatically inferring the format from the file contents.
    • Import["file.gz","format"] uncompresses a GZIP file and imports it as "format".
    • Since the Wolfram Language can automatically recognize all common file-compression and archive formats, it is never necessary to specify "GZIP" as a format in the second argument of Import.
    • Import["file.ext.gz",elem] imports the specified element from file.ext file.
    • Import["file.ext.gz",{elem,suba,subb,}] imports a subelement.
    • Import["file.ext.gz",{{elem1,elem2,}}] imports multiple elements.
    • The import format can be explicitly specified with Import["file.gz",{"format",elems1,elems2,}].
    • Export["file.ext.gz",expr,elem] creates a GZIP-compressed .ext file by treating expr as specifying element elem.
    • Export["file.ext.gz",{expr1,expr2,},{{elem1,elem2,}}] treats each expri as specifying the corresponding elemi.
    • Export["file.ext.gz",expr,opt1->val1,] exports expr with the specified option elements taken to have the specified values.
    • The export format and GZIP compression method can be explicitly specified with Export["file",exprs,{"GZIP","format",elems}].
    • See the reference pages for full general information on Import and Export.
    • ImportString and ExportString can handle GZIP-compressed data.


    • The GZIP format itself does not support Import or Export elements.
    • Import elements of the GZIP-compressed file:
    • "Elements" elements and options available in this file
      "Rules"full rules for each element and option
      "Options"rules for options, properties, and settings
    • Import["file.ext.gz","Elements"] returns the names of elements available in file.ext, effectively unpacking the compressed archive before importing the enclosed file.


    • General Export option:
    • "CompressionLevel"AutomaticGZIP compression strength, given as a number in the range 0 to 1
    • The setting "CompressionLevel"->1 corresponds to the strongest available compression, resulting in the smallest possible file size.


    Basic Examples  (2)

    Export a string to a GZIP-compressed text file:

    Click for copyable input

    Convert the previous output back to a Wolfram Language string:

    Click for copyable input

    This exports graphics to a GZIP-compressed JPEG file:

    Click for copyable input

    This gives the available Import elements of the JPEG file:

    Click for copyable input

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

    Click for copyable input

    See Also

    "BZIP2"  "Directory"  "TAR"  "ZIP"

    Introduced in 2005
    | Updated in 2008