MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / writestr

Method writestr

tools/python-3.11.9-amd64/Lib/zipfile.py:1816–1855  ·  view source on GitHub ↗

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.

(self, zinfo_or_arcname, data,
                 compress_type=None, compresslevel=None)

Source from the content-addressed store, hash-verified

1814 shutil.copyfileobj(src, dest, 1024*8)
1815
1816 def writestr(self, zinfo_or_arcname, data,
1817 compress_type=None, compresslevel=None):
1818 """Write a file into the archive. The contents is 'data', which
1819 may be either a 'str' or a 'bytes' instance; if it is a 'str',
1820 it is encoded as UTF-8 first.
1821 'zinfo_or_arcname' is either a ZipInfo instance or
1822 the name of the file in the archive."""
1823 if isinstance(data, str):
1824 data = data.encode("utf-8")
1825 if not isinstance(zinfo_or_arcname, ZipInfo):
1826 zinfo = ZipInfo(filename=zinfo_or_arcname,
1827 date_time=time.localtime(time.time())[:6])
1828 zinfo.compress_type = self.compression
1829 zinfo._compresslevel = self.compresslevel
1830 if zinfo.filename[-1] == '/':
1831 zinfo.external_attr = 0o40775 << 16 # drwxrwxr-x
1832 zinfo.external_attr |= 0x10 # MS-DOS directory flag
1833 else:
1834 zinfo.external_attr = 0o600 << 16 # ?rw-------
1835 else:
1836 zinfo = zinfo_or_arcname
1837
1838 if not self.fp:
1839 raise ValueError(
1840 "Attempt to write to ZIP archive that was already closed")
1841 if self._writing:
1842 raise ValueError(
1843 "Can't write to ZIP archive while an open writing handle exists."
1844 )
1845
1846 if compress_type is not None:
1847 zinfo.compress_type = compress_type
1848
1849 if compresslevel is not None:
1850 zinfo._compresslevel = compresslevel
1851
1852 zinfo.file_size = len(data) # Uncompressed size
1853 with self._lock:
1854 with self.open(zinfo, mode='w') as dest:
1855 dest.write(data)
1856
1857 def mkdir(self, zinfo_or_directory_name, mode=511):
1858 """Creates a directory inside the zip archive."""

Callers 5

archiveMethod · 0.95
create_archiveFunction · 0.80
__call__Method · 0.80
__call__Method · 0.80
_write_scriptMethod · 0.80

Calls 5

openMethod · 0.95
ZipInfoClass · 0.85
encodeMethod · 0.45
timeMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected