Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,14 +1677,24 @@ def _extract_member(self, member, targetpath, pwd):
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)

# Extract the member
if member.is_dir():
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
return targetpath

with self.open(member, pwd=pwd) as source, \
open(targetpath, "wb") as target:
shutil.copyfileobj(source, target)
else:
with self.open(member, pwd=pwd) as source, \
open(targetpath, "wb") as target:
shutil.copyfileobj(source, target)

# Preserve the permissions if we're on a posix system and the create_system was UNIX
# In the zip specification create_system 3 is UNIX
# See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT CHAPTER 4.4.2.2
zip_create_system_unix = 3
if member.create_system == zip_create_system_unix and os.name == "posix":
# The first 16 bits contain the UNIX permission attributes
# See https://stackoverflow.com/a/46837272
attr = member.external_attr >> 16
os.chmod(targetpath, attr)

return targetpath

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve permissions when extracting zip files on POSIX systems. Only do so if the zip file was created on a Unix-like system.