One function

dir2zip in pypa/auditwheel

The author described this change as fix: files are not compressed with ZIP_DEFLATED (#367). It counts as a record because the check below fails on the code as it stood at 68aad2393 and passes on 4323aeca3, with nothing else changed between the two runs.

Fix saved2022-01-08
Sharing licenceMIT · LICENSE
Change size+8 6

What the code was meant to do, written into the code itself as a docstring

Make a zip file `zip_fname` with contents of directory `in_dir` The recorded filenames are relative to `in_dir`, so doing a standard zip unpack of the resulting `zip_fname` in an empty directory will result in the original directory contents. Parameters ---------- in_dir : str Directory path containing files to go in the zip archive zip_fname : str Filename of zip archive to write date_time : Optional[datetime] Time stamp to set on each file in the archive

The change

1818 st = os.stat(in_dir)
1919 date_time = datetime.fromtimestamp(st.st_mtime, tz=timezone.utc)
2020 date_time_args = date_time.timetuple()[:6]
21- with zipfile.ZipFile(zip_fname, "w", compression=zipfile.ZIP_DEFLATED) as z:
21+ compression = zipfile.ZIP_DEFLATED
22+ with zipfile.ZipFile(zip_fname, "w", compression=compression) as z:
2223 for root, dirs, files in os.walk(in_dir):
2324 for dir in dirs:
2425 dname = os.path.join(root, dir)
2526 out_dname = os.path.relpath(dname, in_dir) + "/"
26- zinfo = zipfile.ZipInfo(out_dname, date_time=date_time_args)
27- zinfo.external_attr = os.stat(dname).st_mode << 16
28- z.writestr(zinfo, "")
27+ zinfo = zipfile.ZipInfo.from_file(dname, out_dname)
28+ zinfo.date_time = date_time_args
29+ z.writestr(zinfo, b"")
2930 for file in files:
3031 fname = os.path.join(root, file)
3132 out_fname = os.path.relpath(fname, in_dir)
32- zinfo = zipfile.ZipInfo(out_fname, date_time=date_time_args)
33- zinfo.external_attr = os.stat(fname).st_mode << 16
33+ zinfo = zipfile.ZipInfo.from_file(fname, out_fname)
34+ zinfo.date_time = date_time_args
35+ zinfo.compress_type = compression
3436 with open(fname, "rb") as fp:
3537 z.writestr(zinfo, fp.read())

The check that tells the two apart

failpass·tests/unit/test_tools.py::test_dir2zip_deflate

Check file tests/unit/test_tools.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it68aad23937225a629d70cb4b3e224abd07f16f44
Broken version dated2022-01-03
Moduleauditwheel.tools
Units changeddir2zip
Fingerprint882df5f8f442e6a2
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in pypa/auditwheel