Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2504cec

Browse files
Issue #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 de5f9f4 commit 2504cec

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lib/shutil.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,16 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
687687
if not dry_run:
688688
with zipfile.ZipFile(zip_filename, "w",
689689
compression=zipfile.ZIP_DEFLATED) as zf:
690+
path = os.path.normpath(base_dir)
691+
zf.write(path, path)
692+
if logger is not None:
693+
logger.info("adding '%s'", path)
690694
for dirpath, dirnames, filenames in os.walk(base_dir):
695+
for name in sorted(dirnames):
696+
path = os.path.normpath(os.path.join(dirpath, name))
697+
zf.write(path, path)
698+
if logger is not None:
699+
logger.info("adding '%s'", path)
691700
for name in filenames:
692701
path = os.path.normpath(os.path.join(dirpath, name))
693702
if os.path.isfile(path):

Lib/test/test_shutil.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,15 +1064,42 @@ def test_make_zipfile(self):
10641064
base_name = os.path.join(work_dir, rel_base_name)
10651065

10661066
with support.change_cwd(work_dir):
1067-
res = make_archive(rel_base_name, 'zip', root_dir, 'dist')
1067+
res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
10681068

10691069
self.assertEqual(res, base_name + '.zip')
10701070
self.assertTrue(os.path.isfile(res))
10711071
self.assertTrue(zipfile.is_zipfile(res))
10721072
with zipfile.ZipFile(res) as zf:
10731073
self.assertCountEqual(zf.namelist(),
1074-
['dist/file1', 'dist/file2', 'dist/sub/file3'])
1074+
['dist/', 'dist/sub/', 'dist/sub2/',
1075+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
10751076

1077+
@requires_zlib
1078+
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
1079+
@unittest.skipUnless(find_executable('zip'),
1080+
'Need the zip command to run')
1081+
def test_zipfile_vs_zip(self):
1082+
root_dir, base_dir = self._create_files()
1083+
base_name = os.path.join(self.mkdtemp(), 'archive')
1084+
archive = make_archive(base_name, 'zip', root_dir, base_dir)
1085+
1086+
# check if ZIP file was created
1087+
self.assertEqual(archive, base_name + '.zip')
1088+
self.assertTrue(os.path.isfile(archive))
1089+
1090+
# now create another ZIP file using `zip`
1091+
archive2 = os.path.join(root_dir, 'archive2.zip')
1092+
zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir]
1093+
with support.change_cwd(root_dir):
1094+
spawn(zip_cmd)
1095+
1096+
self.assertTrue(os.path.isfile(archive2))
1097+
# let's compare both ZIP files
1098+
with zipfile.ZipFile(archive) as zf:
1099+
names = zf.namelist()
1100+
with zipfile.ZipFile(archive2) as zf:
1101+
names2 = zf.namelist()
1102+
self.assertEqual(sorted(names), sorted(names2))
10761103

10771104
def test_make_archive(self):
10781105
tmpdir = self.mkdtemp()
@@ -1183,11 +1210,9 @@ def test_unpack_archive(self):
11831210
formats.append('bztar')
11841211

11851212
root_dir, base_dir = self._create_files()
1213+
expected = rlistdir(root_dir)
1214+
expected.remove('outer')
11861215
for format in formats:
1187-
expected = rlistdir(root_dir)
1188-
expected.remove('outer')
1189-
if format == 'zip':
1190-
expected.remove('dist/sub2/')
11911216
base_name = os.path.join(self.mkdtemp(), 'archive')
11921217
filename = make_archive(base_name, format, root_dir, base_dir)
11931218

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ Core and Builtins
8181
Library
8282
-------
8383

84+
- Issue #24982: shutil.make_archive() with the "zip" format now adds entries
85+
for directories (including empty directories) in ZIP file.
86+
8487
- Issue #25019: Fixed a crash caused by setting non-string key of expat parser.
8588
Based on patch by John Leitch.
8689

0 commit comments

Comments
 (0)