The zipfile
module in Python provides tools for creating, reading, writing, and extracting ZIP files. It allows for easy manipulation of ZIP archives, making it a useful utility for handling compressed files.
Table of Contents
- Introduction
- Key Classes and Methods
ZipFile
ZipInfo
- Examples
- Creating a ZIP File
- Adding Files to a ZIP File
- Extracting Files from a ZIP File
- Listing Contents of a ZIP File
- Reading a Specific File from a ZIP Archive
- Real-World Use Case
- Conclusion
- References
Introduction
The zipfile
module allows for the creation, extraction, and manipulation of ZIP files in Python. It supports reading and writing files in the ZIP archive format, which is widely used for file compression and distribution.
Key Classes and Methods
ZipFile
The ZipFile
class provides methods for creating, reading, writing, and extracting ZIP files.
ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True)
: Opens a ZIP file.open(name, mode='r', pwd=None)
: Opens a file in the ZIP archive.extract(member, path=None, pwd=None)
: Extracts a member from the archive to the current working directory orpath
.extractall(path=None, members=None, pwd=None)
: Extracts all members from the archive to the current working directory orpath
.write(filename, arcname=None, compress_type=None, compresslevel=None)
: Writes a file to the archive.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)
: Writes a string of bytes to the archive.infolist()
: Returns a list ofZipInfo
objects.namelist()
: Returns a list of file names in the archive.testzip()
: Tests the archive for errors.
ZipInfo
The ZipInfo
class provides information about a file in the ZIP archive.
filename
: The name of the file in the archive.date_time
: The time and date of the last modification.compress_type
: The type of compression for the file.file_size
: The size of the file.
Examples
Creating a ZIP File
import zipfile
# Create a new ZIP file
with zipfile.ZipFile('example.zip', 'w') as zipf:
zipf.write('file1.txt')
zipf.write('file2.txt')
Adding Files to a ZIP File
import zipfile
# Add files to an existing ZIP file
with zipfile.ZipFile('example.zip', 'a') as zipf:
zipf.write('file3.txt')
zipf.write('file4.txt')
Extracting Files from a ZIP File
import zipfile
# Extract all files from a ZIP file
with zipfile.ZipFile('example.zip', 'r') as zipf:
zipf.extractall('extracted_files')
Listing Contents of a ZIP File
import zipfile
# List contents of a ZIP file
with zipfile.ZipFile('example.zip', 'r') as zipf:
print(zipf.namelist())
Reading a Specific File from a ZIP Archive
import zipfile
# Read a specific file from a ZIP archive
with zipfile.ZipFile('example.zip', 'r') as zipf:
with zipf.open('file1.txt') as file:
print(file.read().decode())
Real-World Use Case
Archiving Logs
import zipfile
import os
def archive_logs(log_dir, archive_name):
with zipfile.ZipFile(archive_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
for foldername, subfolders, filenames in os.walk(log_dir):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zipf.write(file_path, os.path.relpath(file_path, log_dir))
def extract_logs(archive_name, extract_to):
with zipfile.ZipFile(archive_name, 'r') as zipf:
zipf.extractall(extract_to)
# Archive logs
archive_logs('logs', 'logs_archive.zip')
# Extract logs
extract_logs('logs_archive.zip', 'extracted_logs')
Conclusion
The zipfile
module in Python provides a versatile and powerful way to handle ZIP archives. Whether you need to create, extract, or manipulate ZIP files, the zipfile
module offers all the necessary tools.
Comments
Post a Comment
Leave Comment