Skip to content

Commit 4323aec

Browse files
authored
fix: files are not compressed with ZIP_DEFLATED (#367)
1 parent 68aad23 commit 4323aec

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/auditwheel/tools.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,21 @@ def dir2zip(in_dir: str, zip_fname: str, date_time: Optional[datetime] = None) -
7070
st = os.stat(in_dir)
7171
date_time = datetime.fromtimestamp(st.st_mtime, tz=timezone.utc)
7272
date_time_args = date_time.timetuple()[:6]
73-
with zipfile.ZipFile(zip_fname, "w", compression=zipfile.ZIP_DEFLATED) as z:
73+
compression = zipfile.ZIP_DEFLATED
74+
with zipfile.ZipFile(zip_fname, "w", compression=compression) as z:
7475
for root, dirs, files in os.walk(in_dir):
7576
for dir in dirs:
7677
dname = os.path.join(root, dir)
7778
out_dname = os.path.relpath(dname, in_dir) + "/"
78-
zinfo = zipfile.ZipInfo(out_dname, date_time=date_time_args)
79-
zinfo.external_attr = os.stat(dname).st_mode << 16
80-
z.writestr(zinfo, "")
79+
zinfo = zipfile.ZipInfo.from_file(dname, out_dname)
80+
zinfo.date_time = date_time_args
81+
z.writestr(zinfo, b"")
8182
for file in files:
8283
fname = os.path.join(root, file)
8384
out_fname = os.path.relpath(fname, in_dir)
84-
zinfo = zipfile.ZipInfo(out_fname, date_time=date_time_args)
85-
zinfo.external_attr = os.stat(fname).st_mode << 16
85+
zinfo = zipfile.ZipInfo.from_file(fname, out_fname)
86+
zinfo.date_time = date_time_args
87+
zinfo.compress_type = compression
8688
with open(fname, "rb") as fp:
8789
z.writestr(zinfo, fp.read())
8890

tests/unit/test_tools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,14 @@ def test_zip2dir_round_trip_permissions(tmp_path):
8787
dir2zip(str(tmp_path / "unzip1"), str(tmp_path / "tmp.zip"))
8888
zip2dir(str(tmp_path / "tmp.zip"), str(extract_path))
8989
_check_permissions(extract_path)
90+
91+
92+
def test_dir2zip_deflate(tmp_path):
93+
buffer = b"\0" * 1024 * 1024
94+
input_dir = tmp_path / "input_dir"
95+
input_dir.mkdir()
96+
input_file = input_dir / "zeros.bin"
97+
input_file.write_bytes(buffer)
98+
output_file = tmp_path / "ouput.zip"
99+
dir2zip(str(input_dir), str(output_file))
100+
assert output_file.stat().st_size < len(buffer) / 4

0 commit comments

Comments
 (0)