Skip to content

Instantly share code, notes, and snippets.

@c0xc
Created July 5, 2024 14:17
Show Gist options
  • Save c0xc/b54c005b296cdf6378ce65dd4aff3fe7 to your computer and use it in GitHub Desktop.
Save c0xc/b54c005b296cdf6378ce65dd4aff3fe7 to your computer and use it in GitHub Desktop.
code to demonstrate weird behavior of zipfile module
#!/usr/bin/env python3
import zipfile
from datetime import datetime
import random
import string
zip_file = zipfile.ZipFile("test.zip", "w", compression=zipfile.ZIP_BZIP2, compresslevel=8)
now = datetime.now()
# This will get the default mtime of 1980
with zip_file.open("file1_streamed", mode="w", force_zip64=True) as f:
data = ''.join(random.choices(string.ascii_uppercase, k=1024))
for _ in range(500):
f.write(data.encode())
# This won't be compressed
zip_info = zipfile.ZipInfo("file2_streamed", now.timetuple())
with zip_file.open(zip_info, mode="w", force_zip64=True) as f:
data = ''.join(random.choices(string.ascii_uppercase, k=1024))
for _ in range(500):
f.write(data.encode())
# This won't be compressed either
zip_info = zipfile.ZipInfo.from_file("/etc/os-release", "file3_disk")
with zip_file.open(zip_info, mode="w", force_zip64=True) as f:
with open("/etc/os-release") as in_file:
f.write(in_file.read().encode())
# Works, just for comparison
zip_file.write("/etc/os-release", "file4")
# Workaround - from_file()
zip_info = zipfile.ZipInfo.from_file("/etc/os-release", "file31_disk")
zip_info.compress_type = zip_file.compression
zip_info._compresslevel = zip_file.compresslevel
with zip_file.open(zip_info, mode="w", force_zip64=True) as f:
with open("/etc/os-release") as in_file:
f.write(in_file.read().encode())
# Workaround - streamed file
zip_info = zipfile.ZipInfo("file21_streamed", now.timetuple())
zip_info.compress_type = zip_file.compression
#zip_info._compresslevel = zip_file.compresslevel
with zip_file.open(zip_info, mode="w", force_zip64=True) as f:
data = ''.join(random.choices(string.ascii_uppercase, k=1024))
for _ in range(500):
f.write(data.encode())
# Fix using new method so we don't have to initialize ZipInfo ourselves
#zip_info = zip_file.newinfo("file22_streamed")
#with zip_file.open(zip_info, mode="w", force_zip64=True) as f:
# data = ''.join(random.choices(string.ascii_uppercase, k=1024))
# for _ in range(500):
# f.write(data.encode())
zip_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment