Skip to content
Tutorials Center
My learning

Topics

Lesson 73 of 149 ยท Files and Errors

Zip, Tar and Compression

Create and read archives in Python. Use zipfile, tarfile, gzip and shutil.make_archive, extract files safely and compress data in memory.

Sooner or later a program needs to bundle several files into one, or shrink data before storing or sending it: a backup script, an export button, a downloaded dataset, a log rotation job. Python's standard library handles the common formats without any installation. zipfile reads and writes .zip files, tarfile handles .tar, .tar.gz and friends, gzip compresses a single stream, and shutil.make_archive wraps the most common jobs in one call.

First, two words that people mix up. An archive combines many files and folders into one file (zip and tar do this). Compression makes data smaller (gzip, deflate, bzip2, lzma). A .zip does both at once, while a .tar only archives, so you usually compress it on top, giving .tar.gz. This lesson builds on Files, pathlib and os and shutil.

flowchart LR
    F["Many files"] -- "zipfile" --> Z[".zip<br/>(archive + compression)"]
    F -- "tarfile" --> T[".tar<br/>(archive only)"]
    T -- "gzip / bz2 / lzma" --> TG[".tar.gz / .tar.bz2 / .tar.xz"]
    D["One data stream"] -- "gzip" --> G[".gz"]

Zip combines both steps; tar and compressors are separate layers you can mix.

Note: The editors on this page have their own in-memory file system, and every example works in a temporary folder or in memory, so nothing touches your computer. Output that depends on the compression algorithm (exact compressed sizes) is not printed, since different zlib builds can differ slightly.

Writing and reading a zip file

Open a ZipFile in a with block. Mode "w" creates a new archive, "r" reads one and "a" appends. Compression is off by default, so ask for ZIP_DEFLATED:

Python Create and list a zip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

write(path, arcname=...) copies a file from disk into the archive under the name you choose. Always set arcname, or the archive stores the full path of your file, which is rarely wanted. writestr(name, data) adds content that never existed as a file. Reading is just as direct: namelist() lists names, infolist() gives sizes and dates, read(name) returns the bytes of one member, and open(name) gives a file-like object for large members.

Extracting, and the zip-slip danger

extractall(folder) unpacks everything. extract(name, folder) unpacks one member:

Python Extract a zip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Archives held in a BytesIO object are handy for web apps that build a download without writing to disk.

Warning: Never extract an archive from an untrusted source without checking it. A malicious member named ../../etc/passwd could escape the target folder (the "zip slip" attack). Current Python versions sanitize names in ZipFile.extract(), but your own code that joins archive.namelist() with a folder is on its own. Check that each resolved path stays inside the target, and watch the total size to defend against "zip bombs" that expand to gigabytes.

Python Checking names before extracting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

tar archives

A tar file is a simple stream of files with their permissions and timestamps. Python reads and writes it with tarfile, and the mode string selects compression: "w" none, "w:gz" gzip, "w:bz2", "w:xz". For reading, "r:*" detects the format automatically:

Python Create and read a .tar.gz

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

tar.add() adds a folder with all its contents. When extracting tar files, pass filter="data". It refuses absolute paths, links that point outside the target and special device files. Python 3.14 uses this filter by default, but being explicit makes code readable and safe on older versions too.

gzip: compressing one stream

gzip compresses a single stream of bytes, like a log file. It doesn't store file names or hold several files, which is why tar and gzip are paired. Use gzip.open() like open():

Python gzip files and bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For in-memory data, gzip.compress() and gzip.decompress() do the job in one call. The related modules bz2, lzma and zlib share the same shape: compress(), decompress() and an open() function. lzma (the .xz format) usually compresses best but slowest, gzip is the fast, universal default.

One-call archives with shutil

When you simply want to pack a whole folder, shutil.make_archive() does it in one line, and shutil.unpack_archive() reverses it by detecting the format from the file name:

Python make_archive and unpack_archive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The arguments are the base name (without extension), the format ("zip", "tar", "gztar", "bztar", "xztar") and root_dir, the folder whose contents you want inside the archive. The function returns the full path of the file it created.

Which tool when?

Job Use
Share files with Windows or macOS users zipfile
Back up a folder keeping permissions tarfile (.tar.gz)
Shrink one log or data file gzip
Pack or unpack a whole folder quickly shutil.make_archive / unpack_archive

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between an archive and compression?

    Tar only bundles; gzip only compresses; zip does both. That is why you see .tar.gz.
  2. 2. Which code creates a zip file that actually compresses its members?

    By default ZipFile stores members uncompressed (ZIP_STORED). Passing ZIP_DEFLATED turns compression on.
  3. 3. Why pass filter='data' to tarfile.extractall()?

    Archives from untrusted sources may try to write outside the target folder. The data filter rejects such members.
  4. 4. Which call packs the folder site into release.zip?

    make_archive(base_name, format, root_dir) adds the right extension itself and returns the path it wrote.

Summary

  • Archives bundle files, compression shrinks data; zip does both, tar needs a compressor on top.
  • zipfile.ZipFile writes with write()/writestr(), reads with namelist()/read(), and unpacks with extract()/extractall(). Pass ZIP_DEFLATED to compress.
  • tarfile.open(path, "w:gz") creates .tar.gz; use "r:*" to read any variant and filter="data" when extracting.
  • gzip.open(), gzip.compress() and gzip.decompress() handle single streams and in-memory bytes.
  • shutil.make_archive() and unpack_archive() do whole-folder jobs in one call.
  • Never trust member names or sizes from untrusted archives.

Next: Classes, the start of object-oriented programming in Python.