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

Skip to content

Commit d941d7a

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.
2 parents 6c8b66c + 2504cec commit d941d7a

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
@@ -679,7 +679,16 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
679679
if not dry_run:
680680
with zipfile.ZipFile(zip_filename, "w",
681681
compression=zipfile.ZIP_DEFLATED) as zf:
682+
path = os.path.normpath(base_dir)
683+
zf.write(path, path)
684+
if logger is not None:
685+
logger.info("adding '%s'", path)
682686
for dirpath, dirnames, filenames in os.walk(base_dir):
687+
for name in sorted(dirnames):
688+
path = os.path.normpath(os.path.join(dirpath, name))
689+
zf.write(path, path)
690+
if logger is not None:
691+
logger.info("adding '%s'", path)
683692
for name in filenames:
684693
path = os.path.normpath(os.path.join(dirpath, name))
685694
if os.path.isfile(path):

Lib/test/test_shutil.py

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

10721072
with support.change_cwd(work_dir):
1073-
res = make_archive(rel_base_name, 'zip', root_dir, 'dist')
1073+
res = make_archive(rel_base_name, 'zip', root_dir, base_dir)
10741074

10751075
self.assertEqual(res, base_name + '.zip')
10761076
self.assertTrue(os.path.isfile(res))
10771077
self.assertTrue(zipfile.is_zipfile(res))
10781078
with zipfile.ZipFile(res) as zf:
10791079
self.assertCountEqual(zf.namelist(),
1080-
['dist/file1', 'dist/file2', 'dist/sub/file3'])
1080+
['dist/', 'dist/sub/', 'dist/sub2/',
1081+
'dist/file1', 'dist/file2', 'dist/sub/file3'])
10811082

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

10831110
def test_make_archive(self):
10841111
tmpdir = self.mkdtemp()
@@ -1191,11 +1218,9 @@ def test_unpack_archive(self):
11911218
formats.append('xztar')
11921219

11931220
root_dir, base_dir = self._create_files()
1221+
expected = rlistdir(root_dir)
1222+
expected.remove('outer')
11941223
for format in formats:
1195-
expected = rlistdir(root_dir)
1196-
expected.remove('outer')
1197-
if format == 'zip':
1198-
expected.remove('dist/sub2/')
11991224
base_name = os.path.join(self.mkdtemp(), 'archive')
12001225
filename = make_archive(base_name, format, root_dir, base_dir)
12011226

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Core and Builtins
1414
Library
1515
-------
1616

17+
- Issue #24982: shutil.make_archive() with the "zip" format now adds entries
18+
for directories (including empty directories) in ZIP file.
19+
1720
- Issue #25019: Fixed a crash caused by setting non-string key of expat parser.
1821
Based on patch by John Leitch.
1922

0 commit comments

Comments
 (0)