Fundamentals 10 min read

Master Python File Compression: Zip, Tar.gz, RAR, and 7z Made Easy

This guide explains how to handle common compression formats—zip, tar.gz, rar, and 7z—in Python, covering built‑in modules, third‑party libraries, required installation steps, and complete code examples for both creating and extracting archives.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python File Compression: Zip, Tar.gz, RAR, and 7z Made Easy

Introduction

In daily work, Python is often used to process text files and compressed archives.

Common Compression Formats

rar – widely used on Windows, typically handled with WinRAR.

tar – Linux packaging tool (no compression by itself).

gz – gzip, compresses a single file; combined with tar for tar.gz.

tgz – tar followed by gzip compression.

zip – can compress multiple files, similar algorithm to gzip.

7z – high‑efficiency format supported by 7‑Zip.

Working with ZIP Files

The zipfile module provides ZipFile for creating and reading zip archives and ZipInfo for file metadata.

import os
import zipfile

# Compression
def make_zip(source_dir, output_filename):
    zipf = zipfile.ZipFile(output_filename, 'w')
    pre_len = len(os.path.dirname(source_dir))
    for parent, dirnames, filenames in os.walk(source_dir):
        for filename in filenames:
            pathfile = os.path.join(parent, filename)
            arcname = pathfile[pre_len:].strip(os.path.sep)  # relative path
            zipf.write(pathfile, arcname)
    zipf.close()

# Decompression
def un_zip(file_name):
    """unzip zip file"""
    zip_file = zipfile.ZipFile(file_name)
    if os.path.isdir(file_name + "_files"):
        pass
    else:
        os.mkdir(file_name + "_files")
    for names in zip_file.namelist():
        zip_file.extract(names, file_name + "_files/")
    zip_file.close()

if __name__ == '__main__':
    make_zip(r"E:python_samplelibstest_tar_fileslibs", "test.zip")
    un_zip("test.zip")

Working with TAR.GZ Files

The tarfile module reads and writes tar archives, optionally compressed with gzip, bzip2, or lzma. The mode string follows the pattern filemode[:compression]. Common modes include: r or r:* – read with transparent compression (default). r: – read without compression. r:gz – read gzip‑compressed archive. r:bz2 – read bzip2‑compressed archive. r:xz – read lzma‑compressed archive. x – create an uncompressed tar archive. x:gz – create a gzip‑compressed tar archive. w – write uncompressed. w:gz – write gzip‑compressed.

Alternative stream modes use the | separator (e.g., r|gz for a gzip stream).

import os
import tarfile
import gzip

# Pack an entire directory into a .tar.gz file
def make_targz(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

# Pack files one by one (empty sub‑directories are skipped)
def make_targz_one_by_one(output_filename, source_dir):
    tar = tarfile.open(output_filename, "w:gz")
    for root, dir, files in os.walk(source_dir):
        for file in files:
            pathfile = os.path.join(root, file)
            tar.add(pathfile)
    tar.close()

# Decompress a .gz file
def un_gz(file_name):
    """ungz zip file"""
    f_name = file_name.replace('.gz', '')
    g_file = gzip.GzipFile(file_name)
    open(f_name, "wb+").write(g_file.read())
    g_file.close()

# Decompress a .tar archive
def un_tar(file_name):
    tar = tarfile.open(file_name)
    names = tar.getnames()
    if not os.path.isdir(file_name + "_files"):
        os.mkdir(file_name + "_files")
    for name in names:
        tar.extract(name, file_name + "_files/")
    tar.close()

if __name__ == '__main__':
    make_targz('test.tar.gz', "E:python_samplelibs")
    make_targz_one_by_one('test01.tgz', "E:python_samplelibs")
    un_gz('test.tar.gz')
    un_tar('test.tar')

Handling RAR Files

Python can extract .rar files using the rarfile library, but it requires the official unrar library.

Windows installation

Download UnRARDLL.exe from the RARLab website and install (default path is C:\Program Files (x86)\UnrarDLL).

Add an environment variable UNRAR_LIB_PATH pointing to UnRAR64.dll (64‑bit) or UnRAR.dll (32‑bit).

Run pip install unrar again.

Linux installation

Download the source rarlinux-6.0.0.tar.gz from RARLab.

Extract, compile, and install to produce libunrar.so.

Set UNRAR_LIB_PATH to the location of libunrar.so and run pip install unrar.

# cd /usr/local/src/
# wget https://www.rarlab.com/rar/unrarsrc-6.0.3.tar.gz
# tar zxvf unrarsrc-6.0.3.tar.gz
# cd unrar
# make lib
# make install-lib   # creates libunrar.so
# echo 'export UNRAR_LIB_PATH=/usr/lib/libunrar.so' >> /etc/profile
# source /etc/profile
import rarfile

def unrar(rar_file, dir_name):
    # rarfile needs unrar support; on Linux install via pip, on Windows add unrar.exe to PATH
    rarobj = rarfile.RarFile(rar_file.decode('utf-8'))
    rarobj.extractall(dir_name.decode('utf-8'))

Working with 7Z Files

The py7zr library handles .7z archives.

import py7zr

# Decompression
with py7zr.SevenZipFile("Archive.7z", 'r') as archive:
    archive.extractall(path="/tmp")

# Compression
with py7zr.SevenZipFile("Archive.7z", 'w') as archive:
    archive.writeall("target/")
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythoncompressionzipfiletarfilerarfilepy7zr
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.