Skip to content
Open
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
8 changes: 8 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ def test_writestr_compresslevel(self):
self.assertEqual(b_info.compress_type, self.compression)
self.assertEqual(b_info._compresslevel, 2)

def test_writestr_permission(self):
zipfp = zipfile.ZipFile(TESTFN2, "w")
zipfp.writestr("b.txt", "hello world", file_perm=0o755)
b_info = zipfp.getinfo('b.txt')
perm = oct(b_info.external_attr >> 16)
assert perm == '0o755'


def test_read_return_size(self):
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
# than requested.
Expand Down
8 changes: 5 additions & 3 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,12 +1751,14 @@ def write(self, filename, arcname=None,
shutil.copyfileobj(src, dest, 1024*8)

def writestr(self, zinfo_or_arcname, data,
compress_type=None, compresslevel=None):
compress_type=None, compresslevel=None,
file_perm=0o600):
"""Write a file into the archive. The contents is 'data', which
may be either a 'str' or a 'bytes' instance; if it is a 'str',
it is encoded as UTF-8 first.
'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive."""
the name of the file in the archive. Default permission is set to 600
(rw-------) but can be specified with file_perm."""
if isinstance(data, str):
data = data.encode("utf-8")
if not isinstance(zinfo_or_arcname, ZipInfo):
Expand All @@ -1768,7 +1770,7 @@ def writestr(self, zinfo_or_arcname, data,
zinfo.external_attr = 0o40775 << 16 # drwxrwxr-x
zinfo.external_attr |= 0x10 # MS-DOS directory flag
else:
zinfo.external_attr = 0o600 << 16 # ?rw-------
zinfo.external_attr = file_perm << 16 # ?rw-------
else:
zinfo = zinfo_or_arcname

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add option to zipfile.writestr to open permissions: new optional argument
to allow user to choose permission of written file