Skip to content

Commit fe45f65

Browse files
Issue python#24982: shutil.make_archive() with the "zip" format now adds entries
for directories (including empty directories) in ZIP file. Added test for comparing shutil.make_archive() with the "zip" command.
1 parent f3bdc10 commit fe45f65

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎Lib/shutil.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,16 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
449449
if not dry_run:
450450
with zipfile.ZipFile(zip_filename, "w",
451451
compression=zipfile.ZIP_DEFLATED) as zf:
452+
path = os.path.normpath(base_dir)
453+
zf.write(path, path)
454+
if logger is not None:
455+
logger.info("adding '%s'", path)
452456
for dirpath, dirnames, filenames in os.walk(base_dir):
457+
for name in sorted(dirnames):
458+
path = os.path.normpath(os.path.join(dirpath, name))
459+
zf.write(path, path)
460+
if logger is not None:
461+
logger.info("adding '%s'", path)
453462
for name in filenames:
454463
path = os.path.normpath(os.path.join(dirpath, name))
455464
if os.path.isfile(path):

‎Lib/test/test_shutil.py‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,42 @@ def test_make_zipfile(self):
476476
base_name = os.path.join(work_dir, rel_base_name)
477477

478478
with support.change_cwd(work_dir):
479-
res = make_archive(rel_base_name, 'zip', root_dir, 'dist')
479+
res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
480480

481481
self.assertEqual(res, base_name + '.zip')
482482
self.assertTrue(os.path.isfile(res))
483483
self.assertTrue(zipfile.is_zipfile(res))
484484
with zipfile.ZipFile(res) as zf:
485485
self.assertEqual(sorted(zf.namelist()),
486-
['dist/file1', 'dist/file2', 'dist/sub/file3'])
486+
['dist/', 'dist/file1', 'dist/file2',
487+
'dist/sub/', 'dist/sub/file3', 'dist/sub2/'])
487488

489+
@unittest.skipUnless(zlib, "Requires zlib")
490+
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
491+
@unittest.skipUnless(find_executable('zip'),
492+
'Need the zip command to run')
493+
def test_zipfile_vs_zip(self):
494+
root_dir, base_dir = self._create_files()
495+
base_name = os.path.join(self.mkdtemp(), 'archive')
496+
archive = make_archive(base_name, 'zip', root_dir, base_dir)
497+
498+
# check if ZIP file was created
499+
self.assertEqual(archive, base_name + '.zip')
500+
self.assertTrue(os.path.isfile(archive))
501+
502+
# now create another ZIP file using `zip`
503+
archive2 = os.path.join(root_dir, 'archive2.zip')
504+
zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
505+
with support.change_cwd(root_dir):
506+
spawn(zip_cmd)
507+
508+
self.assertTrue(os.path.isfile(archive2))
509+
# let's compare both ZIP files
510+
with zipfile.ZipFile(archive) as zf:
511+
names = zf.namelist()
512+
with zipfile.ZipFile(archive2) as zf:
513+
names2 = zf.namelist()
514+
self.assertEqual(sorted(names), sorted(names2))
488515

489516
def test_make_archive(self):
490517
tmpdir = self.mkdtemp()

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #24982: shutil.make_archive() with the "zip" format now adds entries
41+
for directories (including empty directories) in ZIP file.
42+
4043
- Issue #17849: Raise a sensible exception if an invalid response is
4144
received for a HTTP tunnel request, as seen with some servers that
4245
do not support tunnelling. Initial patch from Cory Benfield.

0 commit comments

Comments
 (0)