title: Read matlab file (*.mat) from zipped file without extracting to directory in Python
body: <p>This specific questions stems from the attempt to handle large data sets produced by a MATLAB algorithm so that I can process them with python algorithms.</p>
  
  <p><strong>Background:</strong> I have large arrays in MATLAB (typically 20x20x40x15000 [i,j,k,frame]) and I want to use them in python. So I save the array to a *.mat file and use <code>scipy.io.loadmat(fname)</code> to read the *.mat file into a numpy array. However, a problem arises in that if I try to load the entire *.mat file in python, a memory error occurs. To get around this, I slice the *.mat file into pieces, so that I can load the pieces one at a time into a python array. If I divide up the *.mat by frame, I now have 15,000 *.mat files which quickly becomes a pain to work with (at least in windows). So my solution is to use zipped files.</p>
  
  <p><strong>Question:</strong> Can I use scipy to directly read a *.mat file from a zipped file without first unzipping the file to the current working directory?</p>
  
  <p><strong>Specs:</strong> Python 2.7, windows xp </p>
  
  <p><strong>Current code:</strong></p>
  
  <pre><code>import scipy.io
  import zipfile
  import numpy as np
  
  def readZip(zfilename,dim,frames):
      data=np.zeros((dim[0],dim[1],dim[2],frames),dtype=np.float32)
      zfile = zipfile.ZipFile( zfilename, "r" )
      i=0
      for info in zfile.infolist():
          fname = info.filename
          zfile.extract(fname)
          mat=scipy.io.loadmat(fname)
          data[:,:,:,i]=mat['export']
          mat.clear()
          i=i+1
      return data
  </code></pre>
  
  <p><strong>Tried code:</strong></p>
  
  <pre><code>mat=scipy.io.loadmat(zfile.read(fname))
  </code></pre>
  
  <p><em>produces this error:</em></p>
  
  <pre><code>TypeError: file() argument 1 must be encoded string without NULL bytes, not str
  </code></pre>
  
  <hr>
  
  <pre><code>mat=scipy.io.loadmat(zfile.open(fname))
  </code></pre>
  
  <p><em>produces this error:</em></p>
  
  <pre><code>fileobj.seek(0)
  UnsupportedOperation: seek
  </code></pre>
  
  <hr>
  
  <p>Any other suggestions on handling the data are appreciated.</p>
  
  <p>Thanks!</p>