本文整理匯總了Python中zipfile.stringFileHeader方法的典型用法代碼示例。如果您正苦於以下問題:Python zipfile.stringFileHeader方法的具體用法?Python zipfile.stringFileHeader怎麽用?Python zipfile.stringFileHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zipfile
的用法示例。
在下文中一共展示了zipfile.stringFileHeader方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: write
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import stringFileHeader [as 別名]
def write (self, data):
# only test for headers if we know that we're not writing
# (potentially compressed) data.
if self.in_file_data == 0:
if data[:4] == zipfile.stringFileHeader:
# fix the file header for extra Data descriptor
hdr = list (struct.unpack (zipfile.structFileHeader, data[:30]))
hdr[3] |= (1 << 3)
data = struct.pack (zipfile.structFileHeader, *hdr) + data[30:]
self.in_file_data = 1
elif data[:4] == zipfile.stringCentralDir:
# fix the directory entry to match file header.
hdr = list (struct.unpack (zipfile.structCentralDir, data[:46]))
hdr[5] |= (1 << 3)
data = struct.pack (zipfile.structCentralDir, *hdr) + data[46:]
self.position += len (data)
self.victim_fd.write (data)
示例2: readfile
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import stringFileHeader [as 別名]
def readfile(self, name):
"""
Return file-like object for name.
"""
if self.mode not in ("r", "a"):
raise RuntimeError('read() requires mode "r" or "a"')
if not self.fp:
raise RuntimeError(
"Attempt to read ZIP archive that was already closed")
zinfo = self.getinfo(name)
self.fp.seek(zinfo.header_offset, 0)
fheader = self.fp.read(_fileHeaderSize)
if fheader[0:4] != zipfile.stringFileHeader:
raise zipfile.BadZipfile("Bad magic number for file header")
fheader = struct.unpack(zipfile.structFileHeader, fheader)
fname = self.fp.read(fheader[zipfile._FH_FILENAME_LENGTH])
if fheader[zipfile._FH_EXTRA_FIELD_LENGTH]:
self.fp.read(fheader[zipfile._FH_EXTRA_FIELD_LENGTH])
if fname != zinfo.orig_filename:
raise zipfile.BadZipfile(
'File name in directory "%s" and header "%s" differ.' % (
zinfo.orig_filename, fname))
if zinfo.compress_type == zipfile.ZIP_STORED:
return ZipFileEntry(self, zinfo.compress_size)
elif zinfo.compress_type == zipfile.ZIP_DEFLATED:
return DeflatedZipFileEntry(self, zinfo.compress_size)
else:
raise zipfile.BadZipfile(
"Unsupported compression method %d for file %s" %
(zinfo.compress_type, name))
示例3: __init__
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import stringFileHeader [as 別名]
def __init__(self, zip, path):
self._fh = zip.fp
self.url = self._fh.url
self.etag = self._fh.etag
self.last_modified = self._fh.last_modified
self.cookies = self._fh.cookies
# Calculate file offset and length
try:
info = zip.getinfo(path)
except KeyError:
raise BadPackageError('Path "%s" missing from package' % path)
# ZipInfo.extra is the extra field from the central directory file
# header, which may be different from the extra field in the local
# file header. So we need to read the local file header to determine
# its size.
header_fmt = '<4s5H3I2H'
header_len = struct.calcsize(header_fmt)
self._fh.seek(info.header_offset)
magic, _, flags, compression, _, _, _, _, _, name_len, extra_len = \
struct.unpack(header_fmt, self._fh.read(header_len))
if magic != zipfile.stringFileHeader:
raise BadPackageError('Member "%s" has invalid header' % path)
if compression != zipfile.ZIP_STORED:
raise BadPackageError('Member "%s" is compressed' % path)
if flags & 0x1:
raise BadPackageError('Member "%s" is encrypted' % path)
self.offset = info.header_offset + header_len + name_len + extra_len
self.size = info.file_size
示例4: readfile
# 需要導入模塊: import zipfile [as 別名]
# 或者: from zipfile import stringFileHeader [as 別名]
def readfile(self, name):
"""
Return file-like object for name.
"""
if self.mode not in ("r", "a"):
raise RuntimeError('read() requires mode "r" or "a"')
if not self.fp:
raise RuntimeError(
"Attempt to read ZIP archive that was already closed")
zinfo = self.getinfo(name)
self.fp.seek(zinfo.header_offset, 0)
fheader = self.fp.read(_fileHeaderSize)
if fheader[0:4] != zipfile.stringFileHeader:
raise zipfile.BadZipfile("Bad magic number for file header")
fheader = struct.unpack(zipfile.structFileHeader, fheader)
fname = self.fp.read(fheader[zipfile._FH_FILENAME_LENGTH])
if fheader[zipfile._FH_EXTRA_FIELD_LENGTH]:
self.fp.read(fheader[zipfile._FH_EXTRA_FIELD_LENGTH])
if zinfo.flag_bits & 0x800:
# UTF-8 filename
fname_str = fname.decode("utf-8")
else:
fname_str = fname.decode("cp437")
if fname_str != zinfo.orig_filename:
raise zipfile.BadZipfile(
'File name in directory "%s" and header "%s" differ.' % (
zinfo.orig_filename, fname_str))
if zinfo.compress_type == zipfile.ZIP_STORED:
return ZipFileEntry(self, zinfo.compress_size)
elif zinfo.compress_type == zipfile.ZIP_DEFLATED:
return DeflatedZipFileEntry(self, zinfo.compress_size)
else:
raise zipfile.BadZipfile(
"Unsupported compression method %d for file %s" %
(zinfo.compress_type, name))