Python zipfile.structFileHeader() Examples

The following are 5 code examples of zipfile.structFileHeader(). 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 vote down vote up
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 vote down vote up
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 vote down vote up
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: zipstream.py    From Safejumper-for-Desktop with GNU General Public License v2.0 4 votes vote down vote up
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 #5
Source File: zipstream.py    From learn_python3_spider with MIT License 4 votes vote down vote up
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))