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

Method writestr

Lib/zipfile/__init__.py:1989–2020  ·  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

1987 shutil.copyfileobj(src, dest, 1024*8)
1988
1989 def writestr(self, zinfo_or_arcname, data,
1990 compress_type=None, compresslevel=None):
1991 """Write a file into the archive. The contents is 'data', which
1992 may be either a 'str' or a 'bytes' instance; if it is a 'str',
1993 it is encoded as UTF-8 first.
1994 'zinfo_or_arcname' is either a ZipInfo instance or
1995 the name of the file in the archive."""
1996 if isinstance(data, str):
1997 data = data.encode("utf-8")
1998 if isinstance(zinfo_or_arcname, ZipInfo):
1999 zinfo = zinfo_or_arcname
2000 else:
2001 zinfo = ZipInfo(zinfo_or_arcname)._for_archive(self)
2002
2003 if not self.fp:
2004 raise ValueError(
2005 "Attempt to write to ZIP archive that was already closed")
2006 if self._writing:
2007 raise ValueError(
2008 "Can't write to ZIP archive while an open writing handle exists."
2009 )
2010
2011 if compress_type is not None:
2012 zinfo.compress_type = compress_type
2013
2014 if compresslevel is not None:
2015 zinfo.compress_level = compresslevel
2016
2017 zinfo.file_size = len(data) # Uncompressed size
2018 with self._lock:
2019 with self.open(zinfo, mode='w') as dest:
2020 dest.write(data)
2021
2022 def mkdir(self, zinfo_or_directory_name, mode=511):
2023 """Creates a directory inside the zip archive."""

Callers 15

test_getdata_zipfileMethod · 0.95
test_too_many_filesMethod · 0.95
build_alpharep_fixtureFunction · 0.95
huge_zipfileMethod · 0.95
test_malformed_pathsMethod · 0.95

Calls 5

openMethod · 0.95
ZipInfoClass · 0.85
_for_archiveMethod · 0.80
encodeMethod · 0.45
writeMethod · 0.45

Tested by 15

test_getdata_zipfileMethod · 0.76
test_too_many_filesMethod · 0.76
build_alpharep_fixtureFunction · 0.76
huge_zipfileMethod · 0.76
test_malformed_pathsMethod · 0.76