GZIP (.gz)

Background & Context

    • 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 & Export

  • Import["file.gz"] uncompresses file.gz and imports file, automatically inferring the format from the file contents.
  • Import["file.ext.gz",elem] imports the specified element from the uncompressed file "file.ext".
  • Import["file.ext.gz","format"] uncompresses a GZIP file and imports "file.ext" as "format".
  • Import["file.ext.gz",{"format",elements}] imports elements from "file.ext" as "format".
  • Import["file.ext.gz",{elem,suba,subb,}] imports a subelement.
  • The import format can be explicitly specified with Import["file.gz",{"GZIP",}].
  • Export["file.ext.gz",expr] exports any expression expr to a GZIP-compressed file whose format is implied by the file name extension .ext.
  • The export format and GZIP compression method can be explicitly specified with Export["file",expr,{"GZIP","format"}].
  • 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 GZIP format itself does not support Import or Export elements.
  • Import elements of the GZIP-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.gz","Elements"] returns the names of elements available in file.ext, effectively unpacking the compressed file before importing "file.ext".


  • General Export option:
  • CompressionLevelAutomaticGZIP 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.


open allclose all

Basic Examples  (2)

Export a string to a GZIP-compressed text file:

Convert the previous output back to a Wolfram Language string:

This exports graphics to a GZIP-compressed JPEG file:

This gives the available Import elements of the JPEG file:

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

Export Options  (2)

CompressionLevel  (2)

By default, the maximum amount of compression is used:

This is equivalent to CompressionLevel1:

Specify the minimum amount of compression:

Plot file size over increasing compression level:

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: