This answer is very helpful, but it took me a moment to fully understand what rootlen was for. This example uses some longer variable names to help teach what exactly is going on here. Also included is a block to validate the zip file is correct.
import os
import zipfile
src_path = os.path.join('/', 'some', 'src', 'path')
archive_name = 'myZipFile.zip'
archive_path = os.path.join('/', 'some', 'path', archive_name)
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as archive_file:
for dirpath, dirnames, filenames in os.walk(src_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
archive_file_path = os.path.relpath(file_path, src_path)
archive_file.write(file_path, archive_file_path)
with zipfile.ZipFile(archive_path, 'r') as archive_file:
bad_file = zipfile.ZipFile.testzip(archive_file)
if bad_file:
raise zipfile.BadZipFile(
'CRC check failed for {} with file {}'.format(archive_path, bad_file))