-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-4833: Add ZipFile.mkdir #32160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-4833: Add ZipFile.mkdir #32160
Changes from 4 commits
6341b83
5c441ad
cc3c76e
1028df2
702f0d8
4a93d15
0b4877a
0387bfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1772,6 +1772,7 @@ def write(self, filename, arcname=None, | |
| if zinfo.is_dir(): | ||
| zinfo.compress_size = 0 | ||
| zinfo.CRC = 0 | ||
| self.mkdir(zinfo) | ||
| else: | ||
| if compress_type is not None: | ||
| zinfo.compress_type = compress_type | ||
|
|
@@ -1783,23 +1784,6 @@ def write(self, filename, arcname=None, | |
| else: | ||
| zinfo._compresslevel = self.compresslevel | ||
|
|
||
| if zinfo.is_dir(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was marked as resolved, but I do not see an answer to my question.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think write still accepts directory zinfo, now it’s handled on line 1775 by calling mkdir instead of directly inline here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right -- 1175 does handle that. Thanks! A fairly quick scan of the tests doesn't reveal one using |
||
| with self._lock: | ||
| if self._seekable: | ||
| self.fp.seek(self.start_dir) | ||
| zinfo.header_offset = self.fp.tell() # Start of header bytes | ||
| if zinfo.compress_type == ZIP_LZMA: | ||
| # Compressed data includes an end-of-stream (EOS) marker | ||
| zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1 | ||
|
|
||
| self._writecheck(zinfo) | ||
| self._didModify = True | ||
|
|
||
| self.filelist.append(zinfo) | ||
| self.NameToInfo[zinfo.filename] = zinfo | ||
| self.fp.write(zinfo.FileHeader(False)) | ||
| self.start_dir = self.fp.tell() | ||
| else: | ||
| with open(filename, "rb") as src, self.open(zinfo, 'w') as dest: | ||
| shutil.copyfileobj(src, dest, 1024*8) | ||
|
|
||
|
|
@@ -1844,6 +1828,41 @@ def writestr(self, zinfo_or_arcname, data, | |
| with self.open(zinfo, mode='w') as dest: | ||
| dest.write(data) | ||
|
|
||
| def mkdir(self, zinfo_or_directory_name, mode=511): | ||
| """Creates a directory inside the zip archive.""" | ||
| if isinstance(zinfo_or_directory_name, ZipInfo): | ||
| zinfo = zinfo_or_directory_name | ||
| if not zinfo.is_dir(): | ||
| raise ValueError("The given ZipInfo does not describe a directory") | ||
| elif isinstance(zinfo_or_directory_name, str): | ||
| directory_name = zinfo_or_directory_name | ||
| if not directory_name.endswith("/"): | ||
| directory_name += "/" | ||
| zinfo = ZipInfo(directory_name) | ||
| zinfo.compress_size = 0 | ||
| zinfo.CRC = 0 | ||
| zinfo.external_attr = (mode & 0xFFFF) << 16 | ||
| zinfo.file_size = 0 | ||
| zinfo.external_attr |= 0x10 | ||
| else: | ||
| raise TypeError("Expected type str or ZipInfo") | ||
|
|
||
| with self._lock: | ||
| if self._seekable: | ||
| self.fp.seek(self.start_dir) | ||
| zinfo.header_offset = self.fp.tell() # Start of header bytes | ||
| if zinfo.compress_type == ZIP_LZMA: | ||
| # Compressed data includes an end-of-stream (EOS) marker | ||
| zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1 | ||
|
|
||
| self._writecheck(zinfo) | ||
| self._didModify = True | ||
|
|
||
| self.filelist.append(zinfo) | ||
| self.NameToInfo[zinfo.filename] = zinfo | ||
| self.fp.write(zinfo.FileHeader(False)) | ||
| self.start_dir = self.fp.tell() | ||
|
|
||
| def __del__(self): | ||
| """Call the "close()" method in case the user forgot.""" | ||
| self.close() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add ZipFile.mkdir |
Uh oh!
There was an error while loading. Please reload this page.