MCPcopy Index your code
hub / github.com/python/cpython / _extract_member

Method _extract_member

Lib/zipfile/__init__.py:1884–1931  ·  view source on GitHub ↗

Extract the ZipInfo object 'member' to a physical file on the path targetpath.

(self, member, targetpath, pwd)

Source from the content-addressed store, hash-verified

1882 return arcname
1883
1884 def _extract_member(self, member, targetpath, pwd):
1885 """Extract the ZipInfo object 'member' to a physical
1886 file on the path targetpath.
1887 """
1888 if not isinstance(member, ZipInfo):
1889 member = self.getinfo(member)
1890
1891 # build the destination pathname, replacing
1892 # forward slashes to platform specific separators.
1893 arcname = member.filename.replace('/', os.path.sep)
1894
1895 if os.path.altsep:
1896 arcname = arcname.replace(os.path.altsep, os.path.sep)
1897 # interpret absolute pathname as relative, remove drive letter or
1898 # UNC path, redundant separators, "." and ".." components.
1899 arcname = os.path.splitdrive(arcname)[1]
1900 invalid_path_parts = ('', os.path.curdir, os.path.pardir)
1901 arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
1902 if x not in invalid_path_parts)
1903 if os.path.sep == '\\':
1904 # filter illegal characters on Windows
1905 arcname = self._sanitize_windows_name(arcname, os.path.sep)
1906
1907 if not arcname and not member.is_dir():
1908 raise ValueError("Empty filename.")
1909
1910 targetpath = os.path.join(targetpath, arcname)
1911 targetpath = os.path.normpath(targetpath)
1912
1913 # Create all upper directories if necessary.
1914 upperdirs = os.path.dirname(targetpath)
1915 if upperdirs and not os.path.exists(upperdirs):
1916 os.makedirs(upperdirs, exist_ok=True)
1917
1918 if member.is_dir():
1919 if not os.path.isdir(targetpath):
1920 try:
1921 os.mkdir(targetpath)
1922 except FileExistsError:
1923 if not os.path.isdir(targetpath):
1924 raise
1925 return targetpath
1926
1927 with self.open(member, pwd=pwd) as source, \
1928 open(targetpath, "wb") as target:
1929 shutil.copyfileobj(source, target)
1930
1931 return targetpath
1932
1933 def _writecheck(self, zinfo):
1934 """Check for errors before writing a file to the archive."""

Callers 2

extractMethod · 0.95
extractallMethod · 0.95

Calls 12

getinfoMethod · 0.95
openMethod · 0.95
openFunction · 0.50
replaceMethod · 0.45
joinMethod · 0.45
splitMethod · 0.45
is_dirMethod · 0.45
dirnameMethod · 0.45
existsMethod · 0.45
isdirMethod · 0.45
mkdirMethod · 0.45

Tested by

no test coverage detected