zip-files
The `zip-files` Python library provides command-line utilities, `zip-files` and `zip-folder`, for creating ZIP archives. It offers an easy, platform-independent way to compress files and folders, allowing control over the internal root folder structure and compression method. The current version is 0.4.1, and its release cadence has been infrequent, with the last update in April 2021.
Common errors
-
command not found: zip-files
cause The `zip-files` command-line utility is not in your system's PATH, or the package was not installed correctly.fixEnsure the package is installed in an environment whose `Scripts` directory is in your PATH. If using `pipx`, ensure the virtual environment is correctly linked. Reinstall using `pip install zip-files`. -
zipfile.BadZipFile: File is not a zip file
cause Attempting to open a file that is either not a valid ZIP archive or is corrupted, possibly due to an incomplete download or incorrect file extension.fixVerify the file's integrity and ensure it is a complete, valid ZIP archive. Try opening it with a standard unzipping tool. If using the Python API, check the path and permissions. -
zipfile.BadZipFile: Unsupported compression method
cause The ZIP file was created using a compression method (e.g., Deflate64, old proprietary methods) that is not supported by Python's standard `zipfile` module (which `zip-files` relies upon) or the specific `compression` argument used.fixAttempt to re-compress the archive using a widely supported method like 'deflated' (default for `zip-files`). If reading, use a tool that supports the specific compression method, or consider libraries like `zipfile-deflate64` or `zipfile-inflate64` if `DEFLATE64` is the issue.
Warnings
- gotcha The `zip-files` package (hyphenated PyPI name) provides CLI utilities and a thin Python API, but it is distinct from Python's built-in `zipfile` module (underscore name). Many online resources refer to the standard `zipfile` module, which offers a more comprehensive API for ZIP archive manipulation.
- gotcha ZIP files, especially large or corrupted ones, can be prone to extraction errors such as `BadZipFile` or `BadZipfile` (deprecated alias). This can be caused by incomplete downloads, header corruption, or unsupported compression methods if the archive was created by another tool.
- gotcha When creating ZIP files, be mindful of the `root_folder` parameter. If not specified, files are added directly to the archive's root. If a `root_folder` is provided, all included files and folders will reside within that single top-level directory upon extraction. This influences how users interact with the extracted content.
Install
-
pip install zip-files
Imports
- create_zip_file
import zip_files
from zip_files.api import create_zip_file
- ZipFile
from zip_files.api import ZipFile
import zipfile
Quickstart
import os
from zip_files.api import create_zip_file
# Create some dummy files for zipping
with open('file1.txt', 'w') as f:
f.write('Content of file 1')
with open('file2.py', 'w') as f:
f.write('print("Hello from file 2")')
output_zip = 'my_archive.zip'
files_to_zip = ['file1.txt', 'file2.py']
root_folder_in_zip = 'my_data'
print(f"Creating {output_zip} with files in root folder '{root_folder_in_zip}'...")
create_zip_file(output_zip, files_to_zip, root_folder=root_folder_in_zip)
print(f"Successfully created {output_zip}")
# You can also use the command-line utility
# import subprocess
# subprocess.run(['zip-files', '-f', 'another_data', 'another_archive.zip', 'file1.txt', 'file2.py'])
# Cleanup (optional)
os.remove('file1.txt')
os.remove('file2.py')
os.remove(output_zip)