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

Skip to content

Commit 90c24c4

Browse files
Issue #22219: The zipfile module CLI now adds entries for directories
(including empty directories) in ZIP file.
2 parents 84bf989 + 518e71b commit 90c24c4

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lib/zipfile.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,14 +1790,21 @@ def addToZip(zf, path, zippath):
17901790
if os.path.isfile(path):
17911791
zf.write(path, zippath, ZIP_DEFLATED)
17921792
elif os.path.isdir(path):
1793+
if zippath:
1794+
zf.write(path, zippath)
17931795
for nm in os.listdir(path):
17941796
addToZip(zf,
17951797
os.path.join(path, nm), os.path.join(zippath, nm))
17961798
# else: ignore
17971799

17981800
with ZipFile(args[1], 'w') as zf:
1799-
for src in args[2:]:
1800-
addToZip(zf, src, os.path.basename(src))
1801+
for path in args[2:]:
1802+
zippath = os.path.basename(path)
1803+
if not zippath:
1804+
zippath = os.path.basename(os.path.dirname(path))
1805+
if zippath in ('', os.curdir, os.pardir):
1806+
zippath = ''
1807+
addToZip(zf, path, zippath)
18011808

18021809
if __name__ == "__main__":
18031810
main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ Core and Builtins
159159
Library
160160
-------
161161

162+
- Issue #22219: The zipfile module CLI now adds entries for directories
163+
(including empty directories) in ZIP file.
164+
162165
- Issue #22449: In the ssl.SSLContext.load_default_certs, consult the
163166
enviromental variables SSL_CERT_DIR and SSL_CERT_FILE on Windows.
164167

0 commit comments

Comments
 (0)