mete0r_kr avatar mete0r_kr committed 239eddc Draft

support file-like object

fix #8

Comments (0)

Files changed (1)

OleFileIO_PL/OleFileIO_PL.py

     # the fat chain, and load new sectors on demand instead of
     # loading it all in one go.
 
-    def __init__(self, fp, sect, size, offset, sectorsize, fat):
+    def __init__(self, fp, sect, size, offset, sectorsize, fat, filesize):
         """
         Constructor for _OleStream class.
 
         debug('_OleStream.__init__:')
         debug('  sect=%d (%X), size=%d, offset=%d, sectorsize=%d, len(fat)=%d, fp=%s'
             %(sect,sect,size,offset,sectorsize,len(fat), repr(fp)))
-        # for debugging messages, size of file where stream is read:
-        if isinstance(fp, StringIO.StringIO):
-            filesize = len(fp.getvalue())   # file in MiniFAT
-        else:
-            filesize = os.path.getsize(fp.name) # file on disk
         #[PL] To detect malformed documents with FAT loops, we compute the
         # expected number of sectors in the stream:
         unknown_size = False
         #    self.fp = open(filename, "rb")
         #else:
         #    self.fp = filename
+        self.fp.seek(0, 2)
+        try:
+            filesize = self.fp.tell()
+        finally:
+            self.fp.seek(0)
+        self._filesize = filesize
 
         # lists of streams in FAT and MiniFAT, to detect duplicate references
         # (list of indexes of first sectors of each stream)
 
         # calculate the number of sectors in the file
         # (-1 because header doesn't count)
-        filesize = os.path.getsize(filename)
         self.nb_sect = ( (filesize + self.SectorSize-1) / self.SectorSize) - 1
         debug( "Number of sectors in the file: %d" % self.nb_sect )
 
             self.fp.seek(self.sectorsize * (sect+1))
         except:
             debug('getsect(): sect=%X, seek=%d, filesize=%d' %
-                (sect, self.sectorsize*(sect+1), os.path.getsize(self.fp.name)))
+                (sect, self.sectorsize*(sect+1), self._filesize))
             self._raise_defect(DEFECT_FATAL, 'OLE sector index out of range')
         sector = self.fp.read(self.sectorsize)
         if len(sector) != self.sectorsize:
                 self.ministream = self._open(self.root.isectStart,
                     size_ministream, force_FAT=True)
             return _OleStream(self.ministream, start, size, 0,
-                              self.minisectorsize, self.minifat)
+                              self.minisectorsize, self.minifat,
+                              self.ministream.size)
         else:
             # standard stream
             return _OleStream(self.fp, start, size, 512,
-                              self.sectorsize, self.fat)
+                              self.sectorsize, self.fat, self._filesize)
 
 
     def _list(self, files, prefix, node):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.