Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

compress

import compress

The compress library provides in-memory and streaming compression and decompression across several algorithms, plus archive-format support for TAR and ZIP.

Compression methods

MethodSubmoduleEncodeDecodeStreamingNotes
Deflatecompress.deflateyesyesyesRFC 1951 raw compress stream.
Zlibcompress.zlibyesyesyesRFC 1950 wrapper around Deflate; also re-exports Deflate’s GzipDecoder/GzipEncoder as DeflateDecoder/DeflateEncoder.
Gzipcompress.gzipyesyesyesRFC 1952 wrapper around Deflate. GzFile treats a .gz file like a regular file.
Zstandardcompress.zstdyesyesyesPure-Rust implementation (zrip).
LZ4compress.lz4yesyesyesFrame format, via lz4_flex.
Bzip2compress.bzip2yesyesyesPure-Rust implementation (libbz2-rs-sys). BzFile treats a .bz2 file like a regular file. Only decodes the first stream in a bzip2 “multistream” (see the submodule’s own docs).
Brotlicompress.brotliyesyesdecode onlyPure-Rust implementation. The streaming encoder buffers all input and compresses once in finish() rather than incrementally: see BrotliEncoder’s own docs.

Every streaming submodule follows the same shape: a compress()/ decompress() one-shot pair for when the whole buffer is already in memory, plus an Encoder/Decoder class pair (write()/flush()/finish()/reset() and read()/read_exact()/read_all()/read_as_string() respectively) for when the input arrives incrementally or the output needs to be consumed as it’s produced. total_in()/ total_out()/available()/finished() are available on both sides of every streaming class for introspection.

compress.checksum provides CRC-32 and Adler-32, independent of any particular compression method: used internally by zip to validate extracted entries, but also usable standalone.

Archive formats

FormatSubmoduleReadWriteCompression methods
TARcompress.taryesyesnone, gzip, bzip2 (COMPRESS_NONE/COMPRESS_GZIP/COMPRESS_BZIP)
ZIPcompress.zipyesyesstored, deflate, bzip2 (ZIP_STORED/ZIP_DEFLATE/ZIP_BZIP2); zip64 supported for archives/files exceeding the classic 4 GiB limit

Both tar and zip expose a low-level class (tar.Tar, zip.ZipArchive) for building an archive entry by entry or inspecting one in detail, and a pair of one-shot free functions (compress()/extract()) for the common case of “archive this whole file or directory” / “extract this whole archive”. zip.extract() refuses to write outside the destination directory even if the archive contains ../-style path-traversal entry names (a “Zip Slip” attack); a ZIP entry that fails to decode cleanly (an unsupported or encrypted entry, or one whose CRC-32 doesn’t match what the archive claims) is reported per-entry via ZipItem.error rather than silently skipped or allowed to abort the whole read.

TAR does not currently support the LZMA/xz compression method some .tar.xz archives use, nor Brotli (no established TAR convention exists for it). ZIP does not currently support LZMA (method 14), PPMd (method 98), or any of the ZIP encryption schemes (traditional PKWARE or AES): an encrypted entry is reported via ZipItem.error rather than attempted.

The compress API

Every public name in compress, wherever it is declared. Each links to the page that documents it.

NameKindSummary
compress.BEST_COMPRESSIONconstantBest compression level.
compress.BEST_SPEEDconstantBest speed compression.
compress.DEFAULT_COMPRESSIONconstantDefault compression level.
compress.DEFAULT_MEMORY_LEVELconstantDefault memory level
compress.DEFAULT_STRATEGYconstantDefault compression strategy.
compress.FILTEREDconstantFiltered compression strategy.
compress.FIXEDconstantFixed compression strategy.
compress.HUFFMAN_ONLYconstanthuffman only compression strategy
compress.MAX_WBITSconstantMaximum windows bit.
compress.NO_COMPRESSIONconstantNo compression level.
compress.RLEconstantRle compression strategy.
compress.ZlibDecoderclassStreaming zlib decompressor implemention.
compress.ZlibEncoderclassA streaming Deflate encoder.
compress.brotli.BrotliDecoderclassStreaming Brotli decompressor implementation.
compress.brotli.BrotliEncoderclassA streaming Brotli encoder.
compress.brotli.compressfunctionCompress data using Brotli.
compress.brotli.decompressfunctionDecompress Brotli-compressed data.
compress.bzip2.BzFileclassThe BzFile class implements a Bzip2 based I/O system that allows you to treat Bzip2 streams (bytes) as if…
compress.bzip2.Bzip2DecoderclassStreaming bzip2 decompressor implementation.
compress.bzip2.Bzip2EncoderclassA streaming Bzip2 encoder.
compress.bzip2.compressfunctionCompress data using the default options for Bzip2.
compress.bzip2.decompressfunctionDecompress Bzip2-compressed data.
compress.checksum.adler32functionUpdates a running Adler-32 checksum with the bytes buf[0,len-1] and return the updated checksum.
compress.checksum.crc32functionUpdate a running CRC-32 checksum with the bytes buf[0,len-1] and return the updated CRC-32 checksum.
compress.compressfunctionCompress compresses as much data as possible, and stops when the input buffer becomes empty or the output…
compress.decompressfunctionDecompress decompresses as much data as possible, and stops when the input buffer becomes empty or the output…
compress.deflate.DeflateDecoderclassStreaming deflate decompressor implemention.
compress.deflate.DeflateEncoderclassA streaming Deflate encoder.
compress.deflate.compressfunctionCompress data using the default options for Deflate.
compress.deflate.decompressfunctionDecompress a deflated data using default options.
compress.gzip.GzFileclassThe GzFile class implements a GZip based I/O system that allows you use treat Gzip streams (bytes) as if they…
compress.gzip.GzipDecoderclassStreaming gzip decompressor implemention.
compress.gzip.GzipEncoderclassA streaming GZip encoder.
compress.gzip.compressfunctionCompress data using the default options for GZip.
compress.gzip.decompressfunctionDecompress a GZipped data using default options.
compress.lz4.Lz4DecoderclassStreaming reader for decompressing the LZ4 frame format.
compress.lz4.Lz4EncoderclassStreaming lz4 compressor implemention.
compress.lz4.compressfunctionCompress data using the Lz4 block format.
compress.lz4.decompressfunctionDecompress a Lz4 compressed data.
compress.tar.COMPRESS_AUTOconstantAutomatically select and detect compression type (Default).
compress.tar.COMPRESS_BZIPconstantCreate and read archives with the BZip2 compression method.
compress.tar.COMPRESS_GZIPconstantCreate and read archives with the GZip compression method.
compress.tar.COMPRESS_NONEconstantCreate and read archives without any compression.
compress.tar.Tarclass
compress.tar.TarCorruptedErrorclassError thrown when a TAR archive is corrupted.
compress.tar.TarIOErrorclassError thrown when an I/O error occurs.
compress.tar.TarIllegalCompressionErrorclassError thrown when an illegal compression type is used.
compress.tar.compressfunctionCreate a new TAR ball from the file or directory in the given path and saves it to the destination path or…
compress.tar.extractfunctionExtracts a TAR file to the given destination or to the same directory as the source file with the same name…
compress.zip.ZIP_BZIP2constantCompression method that indicates Bzip2 compression
compress.zip.ZIP_DEFLATEconstantCompression method that indicates zlib Deflate compression
compress.zip.ZIP_EXTconstantThe default zip file extension
compress.zip.ZIP_FILE_COUNT_LIMITconstantThe maximum number of files in a zip archive when zip64 is not used
compress.zip.ZIP_FILE_MAXconstant
compress.zip.ZIP_MAXconstantThe maximum size of a zip archive when zip64 is not used
compress.zip.ZIP_STOREDconstantCompression method that indicates no compression
compress.zip.ZipArchiveclassZipArchive provides a class for zip archive creation, manipulation and extraction.
compress.zip.ZipFileclassZipFile represents an instance of zip file.
compress.zip.ZipItemclassZipItem represents a single file or directory in a zip archive.
compress.zip.compressfunctionCompresses the given path (file or directory) into the destination zip archive.
compress.zip.extractfunctionExtracts the zip archive at the file path to the given destination directory.
compress.zstd.ZstdDecoderclassStreaming zstd decompressor implemention.
compress.zstd.ZstdEncoderclassStreaming zstd compressor implemention.
compress.zstd.compressfunctionCompress data using the default options for Zstd.
compress.zstd.decompressfunctionDecompress a Zstd compressed data.

Submodules

ModuleReached asSummary
compress.brotlicompress.brotli.*The Brotli submodule for the compress module.
compress.bzip2compress.bzip2.*The Bzip2 submodule for the compress module.
compress.checksumcompress.checksum.*This is the checksum submodule for the compress module.
compress.deflatecompress.deflate.*This is the Deflate submodule for the compress module.
compress.gzipcompress.gzip.*The is the GZip submodule for the compress module.
compress.lz4compress.lz4.*This is the Lz4 submodule for the compress module.
compress.tarcompress.tar.*This module adds support for creating and extracting TAR archives.
compress.zipcompress.zip.*The zip module contains classes and functions to make working with zip archives easy.
compress.zlibcompress.*This is the Zlib submodule for the compress module.
compress.zstdcompress.zstd.*This is the Zstd submodule for the compress module.

1995-2017 Jean-loup Gailly and Mark Adler