Python zipfile.stringFileHeader() Examples
The following are 6
code examples of zipfile.stringFileHeader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
zipfile
, or try the search function
.
Example #1
Source File: woof.py From d4rkc0de with GNU General Public License v2.0 | 6 votes |
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)
Example #2
Source File: woof.py From darkc0de-old-stuff with GNU General Public License v3.0 | 6 votes |
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)
Example #3
Source File: zipstream.py From python-for-android with Apache License 2.0 | 5 votes |
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))
Example #4
Source File: package.py From elijah-provisioning with Apache License 2.0 | 5 votes |
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
Example #5
Source File: zipstream.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 4 votes |
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))
Example #6
Source File: zipstream.py From learn_python3_spider with MIT License | 4 votes |
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))