SoFunction
Updated on 2025-03-10

Implementation of Python reading ZIP and TAR format compression packages

In data processing and file management, compressed packages (such as ZIP, TAR, etc.) are very common. Python provides a variety of libraries to read and process these compressed packages. This article will explain how to use Python's built-in libraries and third-party libraries to read compressed packages in ZIP and TAR formats.

1. Read ZIP file

Python's zipfile module provides the function of processing ZIP files. Here is a simple example showing how to read a ZIP file and extract the contents in it.

Step 1: Import the zipfile module

import zipfile

Step 2: Open the ZIP file

zip_path = ''  # Replace with your ZIP file pathwith (zip_path, 'r') as zip_ref:
    # List all file names in the ZIP file    file_names = zip_ref.namelist()
    print("Files in ZIP:")
    for file_name in file_names:
        print(file_name)

Step 3: Extract the file

# Extract all files to the current directoryzip_ref.extractall()
 
# Or extract a single file# zip_ref.extract('specific_file.txt', path='output_directory')

Step 4: Read the file content

# Read a file content in a ZIP filewith zip_ref.open('specific_file.txt') as file:
    content = ()
    print(('utf-8'))  # Decode to a string

2. Read the TAR file

For TAR files, Python's tarfile module provides corresponding functions. Here is a simple example showing how to read a TAR file and extract the contents in it.

Step 1: Import the tarfile module

import tarfile

Step 2: Open the TAR file

tar_path = ''  # Replace with your TAR file pathwith (tar_path, 'r:*') as tar_ref:
    # List all file names in the TAR file    file_names = tar_ref.getnames()
    print("Files in TAR:")
    for file_name in file_names:
        print(file_name)

Step 3: Extract the file

# Extract all files to the current directorytar_ref.extractall()
 
# Or extract a single file# tar_ref.extract('specific_file.txt', path='output_directory')

Step 4: Read the file content

# Read the contents of a file in the TAR filewith tar_ref.extractfile('specific_file.txt') as file:
    content = ()
    print(('utf-8'))  # Decode to a string

3. Things to note

File paths: Make sure that the provided file paths are correct and that Python scripts have permission to access these paths.
Encoding: When reading the file contents, make sure to use the correct encoding (such as UTF-8). If the file uses other encodings, it needs to be adjusted accordingly.
Exception handling: In actual applications, it is recommended to add exception handling logic to deal with problems such as the non-existence of the file and insufficient permissions.

4. Sample code summary

Here is the complete sample code showing how to read ZIP and TAR files:

import zipfile
import tarfile
 
# Read ZIP filedef read_zip(zip_path):
    with (zip_path, 'r') as zip_ref:
        file_names = zip_ref.namelist()
        print("Files in ZIP:")
        for file_name in file_names:
            print(file_name)
        
        # Extract all files to the current directory        zip_ref.extractall()
        
        # Read specific file contents        with zip_ref.open('specific_file.txt') as file:
            content = ()
            print(('utf-8'))
 
# Read the TAR filedef read_tar(tar_path):
    with (tar_path, 'r:*') as tar_ref:
        file_names = tar_ref.getnames()
        print("Files in TAR:")
        for file_name in file_names:
            print(file_name)
        
        # Extract all files to the current directory        tar_ref.extractall()
        
        # Read specific file contents        with tar_ref.extractfile('specific_file.txt') as file:
            content = ()
            print(('utf-8'))
 


# Example usagezip_path = ''
tar_path = ''
 
read_zip(zip_path)
read_tar(tar_path)

With the above steps, you can easily read and process compressed packages in ZIP and TAR formats using Python.

This is the end of this article about the implementation of Python's ZIP and TAR format compression packages. For more related contents of Python's ZIP and TAR compression packages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!