杰瑞科技汇

python zipfile rar

Of course! Let's break down how to work with archives in Python, specifically focusing on the built-in zipfile module and the common need for rar files.

The zipfile Module (Built-in)

Python comes with a powerful and easy-to-use module called zipfile for handling .zip archives. You don't need to install anything extra.

Here are the most common operations.

A. Creating a ZIP File

You can create a new archive or add files to an existing one.

import zipfile
import os
# Let's create some dummy files to add to our zip
with open("file1.txt", "w") as f:
    f.write("This is the content of file 1.")
with open("file2.txt", "w") as f:
    f.write("This is the content of file 2.")
# --- Creating a new ZIP archive ---
# The 'w' mode means "write" and will create a new zip file.
with zipfile.ZipFile("my_archive.zip", "w") as zipf:
    # Add a single file
    zipf.write("file1.txt", arcname="data/file1.txt") # arcname renames it inside the zip
    # Add another file
    zipf.write("file2.txt", arcname="data/file2.txt")
print("Created my_archive.zip")
# --- Adding files to an existing ZIP archive ---
# The 'a' mode means "append".
with zipfile.ZipFile("my_archive.zip", "a") as zipf:
    zipf.write("file1.txt", arcname="backup/file1_backup.txt")
print("Added a file to my_archive.zip")
# Clean up dummy files
os.remove("file1.txt")
os.remove("file2.txt")

B. Extracting a ZIP File

You can extract all files or just specific ones.

import zipfile
import os
# Create a directory to extract to if it doesn't exist
extract_dir = "extracted_files"
if not os.path.exists(extract_dir):
    os.makedirs(extract_dir)
# --- Extracting all files ---
# The 'r' mode means "read" (it's the default).
with zipfile.ZipFile("my_archive.zip", "r") as zipf:
    zipf.extractall(extract_dir) # Extracts everything to the specified directory
print(f"Extracted all files to '{extract_dir}'")
# --- Extracting a single file ---
with zipfile.ZipFile("my_archive.zip", "r") as zipf:
    zipf.extract("data/file1.txt", path=extract_dir) # Extracts only one file
print(f"Extracted 'data/file1.txt' to '{extract_dir}'")

C. Listing Contents of a ZIP File

You can get a list of all files and directories inside the archive.

import zipfile
with zipfile.ZipFile("my_archive.zip", "r") as zipf:
    # The namelist() method returns a list of file names
    file_list = zipf.namelist()
    print("Contents of my_archive.zip:")
    for file_name in file_list:
        print(f"- {file_name}")
# Example Output:
# Contents of my_archive.zip:
# - data/file1.txt
# - data/file2.txt
# - backup/file1_backup.txt

D. Checking if a File is in a ZIP

This is very useful for selectively extracting files.

import zipfile
file_to_check = "data/file1.txt"
with zipfile.ZipFile("my_archive.zip", "r") as zipf:
    if file_to_check in zipf.namelist():
        print(f"'{file_to_check}' is in the archive.")
    else:
        print(f"'{file_to_check}' is NOT in the archive.")

Handling .rar Files

Python's standard library does not have a built-in module for .rar files. The .rar format is proprietary, and you need an external program to handle it.

The most common approach is to use a Python library that acts as a wrapper for the command-line tool unrar.

Step 1: Install the unrar Command-Line Tool

First, you need the unrar program itself on your system.

  • On Debian/Ubuntu:
    sudo apt-get update
    sudo apt-get install unrar
  • On macOS (using Homebrew):
    brew install unrar
  • On Windows:
    1. Download the unrar executable from the official RARLAB website.
    2. Place the unrar.exe file in a directory that is in your system's PATH environment variable, or remember its full path.

Step 2: Install the Python Wrapper Library

Now, install the Python library that will call the unrar command-line tool for you. The most popular one is rarfile.

pip install rarfile

Step 3: Use the rarfile Library

The rarfile API is designed to be similar to the built-in zipfile module, making it easy to switch between them.

import rarfile
import os
# Let's assume you have a file named 'my_archive.rar'
# Create a dummy rar file for this example (you would use a real one)
# This command requires the 'unrar' tool to be installed in your system.
# In a real script, you would just have the .rar file.
# os.system("rar a my_archive.rar file1.txt file2.txt") # This is for demonstration
# --- Extracting a RAR file ---
# The 'r' mode means "read".
try:
    with rarfile.RarFile("my_archive.rar", "r") as rf:
        # Extract all files
        rf.extractall("extracted_rar_files")
        print("Successfully extracted my_archive.rar")
        # List contents
        print("\nContents of my_archive.rar:")
        for name in rf.namelist():
            print(f"- {name}")
        # Extract a single file
        rf.extract("file1.txt", path="extracted_rar_files/single_file")
        print("\nSuccessfully extracted 'file1.txt' to a specific location.")
except rarfile.NeedFirstVolume:
    print("Error: This is a multi-volume archive, and the first volume is missing.")
except rarfile.BadRarFile:
    print("Error: The file is not a valid RAR archive or is corrupted.")
except FileNotFoundError:
    print("Error: The 'unrar' command-line tool was not found. Please install it.")
# Clean up dummy files if you created them
# if os.path.exists("my_archive.rar"):
#     os.remove("my_archive.rar")

Summary and Comparison

Feature zipfile (for .zip) rarfile (for .rar)
Library Built into Python Requires pip install rarfile
Dependencies None Requires the unrar command-line tool to be installed on the system
Creation Yes (zipf.write()) No. rarfile can only extract, not create .rar archives.
Extraction Yes (zipf.extractall()) Yes (rf.extractall())
Listing Yes (zipf.namelist()) Yes (rf.namelist())
API Style Standard Pythonic API Similar to zipfile for ease of use

When to Use What?

  • Use zipfile for all your .zip file needs. It's robust, requires no setup, and is perfect for creating, reading, and writing zip archives.
  • Use rarfile when you absolutely need to extract .rar files that someone has given you. Remember that it cannot create them, and it relies on an external program (unrar) being correctly installed on the machine where your script will run.
分享:
扫描分享到社交APP
上一篇
下一篇