本文整理匯總了Python中zipfile.ZipInfo方法的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.ZipInfo方法的具體用法?Python zipfile.ZipInfo怎麽用?Python zipfile.ZipInfo使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zipfile
的用法示例。
在下文中一共展示了zipfile.ZipInfo方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: next
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def next(self):
"""Returns the next input from this input reader as (ZipInfo, opener) tuple.
Returns:
The next input from this input reader, in the form of a 2-tuple.
The first element of the tuple is a zipfile.ZipInfo object.
The second element of the tuple is a zero-argument function that, when
called, returns the complete body of the file.
"""
if not self._zip:
self._zip = zipfile.ZipFile(self._reader(self._blob_key))
# Get a list of entries, reversed so we can pop entries off in order
self._entries = self._zip.infolist()[self._start_index:self._end_index]
self._entries.reverse()
if not self._entries:
raise StopIteration()
entry = self._entries.pop()
self._start_index += 1
return (entry, lambda: self._read(entry))
示例2: _read
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def _read(self, entry):
"""Read entry content.
Args:
entry: zip file entry as zipfile.ZipInfo.
Returns:
Entry content as string.
"""
start_time = time.time()
content = self._zip.read(entry.filename)
ctx = context.get()
if ctx:
operation.counters.Increment(COUNTER_IO_READ_BYTES, len(content))(ctx)
operation.counters.Increment(
COUNTER_IO_READ_MSEC, int((time.time() - start_time) * 1000))(ctx)
return content
示例3: _write_to_zip
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def _write_to_zip(self, wheel, rel_path):
sio = StringIO()
yield sio
# The default is a fixed timestamp rather than the current time, so
# that building a wheel twice on the same computer can automatically
# give you the exact same result.
date_time = (2016, 1, 1, 0, 0, 0)
zi = zipfile.ZipInfo(rel_path, date_time)
zi.external_attr = (0o644 & 0xFFFF) << 16 # Unix attributes
b = sio.getvalue().encode("utf-8")
hashsum = hashlib.sha256(b)
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")
wheel.writestr(zi, b, compress_type=zipfile.ZIP_DEFLATED)
self._records.append((rel_path, hash_digest, len(b)))
示例4: _write_to_zip
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def _write_to_zip(self, wheel, rel_path):
sio = StringIO()
yield sio
# The default is a fixed timestamp rather than the current time, so
# that building a wheel twice on the same computer can automatically
# give you the exact same result.
date_time = (2016, 1, 1, 0, 0, 0)
zi = zipfile.ZipInfo(rel_path, date_time)
b = sio.getvalue().encode("utf-8")
hashsum = hashlib.sha256(b)
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")
wheel.writestr(zi, b, compress_type=zipfile.ZIP_DEFLATED)
stream.echo(f" - Adding: {rel_path}", verbosity=stream.DETAIL)
self._records.append((rel_path, hash_digest, str(len(b))))
示例5: test_unsupportedCompression
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_unsupportedCompression(self):
"""
A zipfile which describes an unsupported compression mechanism should
raise BadZipfile.
"""
fn = self.mktemp()
with zipfile.ZipFile(fn, "w") as zf:
zi = zipfile.ZipInfo("0")
zf.writestr(zi, "some data")
# Mangle its compression type in the central directory; can't do
# this before the writestr call or zipfile will (correctly) tell us
# not to pass bad compression types :)
zi.compress_type = 1234
with zipstream.ChunkingZipFile(fn) as czf:
self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
示例6: close
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def close(self):
# Write RECORD
if self.fp is not None and self.mode == 'w' and self._file_hashes:
data = StringIO()
writer = csv.writer(data, delimiter=',', quotechar='"', lineterminator='\n')
writer.writerows((
(
fname,
algorithm + "=" + hash_,
self._file_sizes[fname]
)
for fname, (algorithm, hash_) in self._file_hashes.items()
))
writer.writerow((format(self.record_path), "", ""))
zinfo = ZipInfo(native(self.record_path), date_time=get_zipinfo_datetime())
zinfo.compress_type = ZIP_DEFLATED
zinfo.external_attr = 0o664 << 16
self.writestr(zinfo, as_bytes(data.getvalue()))
ZipFile.close(self)
示例7: dir2zip
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def dir2zip(in_dir, zip_fname):
""" Make a zip file `zip_fname` with contents of directory `in_dir`
The recorded filenames are relative to `in_dir`, so doing a standard zip
unpack of the resulting `zip_fname` in an empty directory will result in
the original directory contents.
Parameters
----------
in_dir : str
Directory path containing files to go in the zip archive
zip_fname : str
Filename of zip archive to write
"""
z = zipfile.ZipFile(zip_fname, 'w',
compression=zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(in_dir):
for file in files:
in_fname = pjoin(root, file)
in_stat = os.stat(in_fname)
# Preserve file permissions, but allow copy
info = zipfile.ZipInfo(in_fname)
info.filename = relpath(in_fname, in_dir)
if os.path.sep == '\\':
# Make the path unix friendly on windows.
# PyPI won't accept wheels with windows path separators
info.filename = relpath(in_fname, in_dir).replace('\\', '/')
# Set time from modification time
info.date_time = time.localtime(in_stat.st_mtime)
# See https://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zip/48435482#48435482 # noqa: E501
# Also set regular file permissions
perms = stat.S_IMODE(in_stat.st_mode) | stat.S_IFREG
info.external_attr = perms << 16
with open_readable(in_fname, 'rb') as fobj:
contents = fobj.read()
z.writestr(info, contents, zipfile.ZIP_DEFLATED)
z.close()
示例8: _info_name
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def _info_name(info):
"""
Returns a normalized file path for an archive info object
:param info:
An info object from _list_archive_members()
:return:
A unicode string with all directory separators normalized to "/"
"""
if isinstance(info, zipfile.ZipInfo):
return info.filename.replace('\\', '/')
return info.name.replace('\\', '/')
示例9: _write_content
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def _write_content(self, archive, path: str, content: str) -> None:
content = content.encode('utf-8')
# write content into archive
zip_info = ZipInfo(path)
archive.writestr(zip_info, content, compress_type=ZIP_DEFLATED)
# calculate hashsum
digest = sha256(content).digest()
digest = urlsafe_b64encode(digest).decode().rstrip('=')
self._records.append((path, digest, len(content)))
示例10: open
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def open(self, name_or_info, mode="r", pwd=None):
"""Return file-like object for 'name'."""
# A non-monkey-patched version would contain most of zipfile.py
ef = zipfile.ZipFile.open(self, name_or_info, mode, pwd)
if isinstance(name_or_info, zipfile.ZipInfo):
name = name_or_info.filename
else:
name = name_or_info
if (name in self._expected_hashes
and self._expected_hashes[name] != None):
expected_hash = self._expected_hashes[name]
try:
_update_crc_orig = ef._update_crc
except AttributeError:
warnings.warn('Need ZipExtFile._update_crc to implement '
'file hash verification (in Python >= 2.7)')
return ef
running_hash = self._hash_algorithm()
if hasattr(ef, '_eof'): # py33
def _update_crc(data):
_update_crc_orig(data)
running_hash.update(data)
if ef._eof and running_hash.digest() != expected_hash:
raise BadWheelFile("Bad hash for file %r" % ef.name)
else:
def _update_crc(data, eof=None):
_update_crc_orig(data, eof=eof)
running_hash.update(data)
if eof and running_hash.digest() != expected_hash:
raise BadWheelFile("Bad hash for file %r" % ef.name)
ef._update_crc = _update_crc
elif self.strict and name not in self._expected_hashes:
raise BadWheelFile("No expected hash for file %r" % ef.name)
return ef
示例11: test_archive_dir
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_archive_dir(self):
builder = Builder('test.zip')
builder._archive_dir(self.zipfile, self.pj_root)
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
示例12: test_archive_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_archive_file(self):
builder = Builder('test.zip')
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'README.md'))
ok_(isinstance(self.zipfile.getinfo('README.md'), zipfile.ZipInfo))
示例13: test_archive_dist
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_archive_dist(self):
builder = Builder('test.zip')
builder._archive_dist(self.zipfile, 'lamvery.js')
ok_(isinstance(self.zipfile.getinfo('lamvery.js'), zipfile.ZipInfo))
示例14: test_archive_single_file_key_error
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_archive_single_file_key_error(self):
self._single_file = True
builder = Builder('test.zip', single_file=True)
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.pyc'), zipfile.ZipInfo))
示例15: test_archive_single_file
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import ZipInfo [as 別名]
def test_archive_single_file(self):
self._single_file = True
builder = Builder('test.zip', single_file=True)
builder._archive_file(
self.zipfile, os.path.join(self.pj_root, 'setup.py'))
ok_(isinstance(self.zipfile.getinfo('setup.py'), zipfile.ZipInfo))