Here are Python code examples for compressing and decompressing files using the zipfile module:
1. Compressing Files into a ZIP Archive
This example shows how to compress multiple files into a single .zip file.
Copy
import zipfile
def compress_files(zip_filename, file_list):
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file in file_list:
zipf.write(file)
print(f"Compressed {file}")
# Example usage
files_to_compress = ['file1.txt', 'file2.txt', 'image.jpg']
compress_files('archive.zip', files_to_compress)
2. Adding Files to an Existing ZIP Archive
You can append files to an existing .zip file instead of overwriting it.
Copy
3. Extracting All Files from a ZIP Archive
This example demonstrates how to extract all files from a .zip archive to a specific directory.
Copy
4. Extracting Specific Files from a ZIP Archive
If you only want to extract specific files from a .zip archive, you can use the extract() method.
Copy
5. Listing the Contents of a ZIP Archive
You can list all the files inside a .zip archive without extracting them.
Copy
6. Compressing a Directory into a ZIP Archive
To compress an entire directory into a .zip file, you need to walk through the directory and add each file individually.
Copy
7. Creating a Password-Protected ZIP Archive
Although the zipfile module doesn't natively support password protection, this example uses a workaround by using the pyzipper module, which supports encrypted .zip files.
Copy
(Note: You need to install pyzipper using pip install pyzipper to use this functionality.)
8. Reading File Information in a ZIP Archive
You can read file metadata (like size, date) from a .zip file without extracting them.
Copy
9. Extracting Files from a ZIP Archive Using Password
If the .zip file is encrypted, you can extract files by providing the password.
Copy
10. Check if File Exists in ZIP Archive
You can check whether a specific file exists in a .zip archive before extracting it.
import zipfile
def append_to_zip(zip_filename, file_list):
with zipfile.ZipFile(zip_filename, 'a', zipfile.ZIP_DEFLATED) as zipf:
for file in file_list:
zipf.write(file)
print(f"Added {file} to the archive")
# Example usage
files_to_add = ['new_file.txt']
append_to_zip('archive.zip', files_to_add)
import zipfile
def extract_all_files(zip_filename, extract_to):
with zipfile.ZipFile(zip_filename, 'r') as zipf:
zipf.extractall(extract_to)
print(f"Extracted all files to {extract_to}")
# Example usage
extract_all_files('archive.zip', 'extracted_files')
import zipfile
def extract_specific_file(zip_filename, file_name, extract_to):
with zipfile.ZipFile(zip_filename, 'r') as zipf:
zipf.extract(file_name, extract_to)
print(f"Extracted {file_name} to {extract_to}")
# Example usage
extract_specific_file('archive.zip', 'file1.txt', 'extracted_files')
import zipfile
def list_zip_contents(zip_filename):
with zipfile.ZipFile(zip_filename, 'r') as zipf:
file_list = zipf.namelist()
print(f"Contents of {zip_filename}: {file_list}")
# Example usage
list_zip_contents('archive.zip')
import zipfile
import os
def zip_directory(directory, zip_filename):
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for foldername, subfolders, filenames in os.walk(directory):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zipf.write(file_path, os.path.relpath(file_path, directory))
print(f"Compressed {file_path}")
# Example usage
zip_directory('my_folder', 'folder_archive.zip')
import pyzipper
def compress_with_password(zip_filename, file_list, password):
with pyzipper.AESZipFile(zip_filename, 'w', encryption=pyzipper.WZ_AES) as zipf:
zipf.setpassword(password.encode())
for file in file_list:
zipf.write(file)
print(f"Compressed {file} with password")
# Example usage
files_to_compress = ['file1.txt', 'file2.txt']
compress_with_password('secure_archive.zip', files_to_compress, 'my_secure_password')
import zipfile
def read_zip_info(zip_filename):
with zipfile.ZipFile(zip_filename, 'r') as zipf:
for file_info in zipf.infolist():
print(f"File: {file_info.filename}, Size: {file_info.file_size} bytes")
# Example usage
read_zip_info('archive.zip')
import pyzipper
def extract_with_password(zip_filename, password, extract_to):
with pyzipper.AESZipFile(zip_filename, 'r') as zipf:
zipf.setpassword(password.encode())
zipf.extractall(extract_to)
print(f"Extracted all files to {extract_to} with password")
# Example usage
extract_with_password('secure_archive.zip', 'my_secure_password', 'extracted_files')
import zipfile
def check_file_in_zip(zip_filename, file_name):
with zipfile.ZipFile(zip_filename, 'r') as zipf:
if file_name in zipf.namelist():
print(f"{file_name} found in the archive")
else:
print(f"{file_name} not found in the archive")
# Example usage
check_file_in_zip('archive.zip', 'file1.txt')