title: Python zipfile, bizarre limit to number of files: "folder is invalid"
body: <p>The computer is toying with me, I know it!</p>
  
  <p>I am creating a zip folder in Python. The individual files are generated in memory and then the whole thing is zipped and saved to a file. I am allowed to add 9 files to the zip. I am allowed to add 11 files to the zip. But 10, no, not 10 files. The zip file IS saved to my computer, but I'm not allowed to open it; Windows says that the compressed zipped folder is invalid.</p>
  
  <p>I use the code below, which I got from another stackoverflow question. It appends 10 files and saves the zipped folder. When I click on the folder, I cannot extract it. BUT, remove one of the appends() and it's fine. Or, add another append and it works!</p>
  
  <p>What am I missing here? How can I make this work every time?  </p>
  
  <pre><code>imz = InMemoryZip() 
  imz.append("1a.txt", "a").append("2a.txt", "a").append("3a.txt", "a").append("4a.txt", "a").append("5a.txt", "a").append("6a.txt", "a").append("7a.txt", "a").append("8a.txt", "a").append("9a.txt", "a").append("10a.txt", "a")
  imz.writetofile("C:/path/test.zip") 
  
  
  import zipfile
  import StringIO
  class InMemoryZip(object):
      def __init__(self):
          # Create the in-memory file-like object
          self.in_memory_zip = StringIO.StringIO()
  
      def append(self, filename_in_zip, file_contents):
          '''Appends a file with name filename_in_zip and contents of 
          file_contents to the in-memory zip.'''
          # Get a handle to the in-memory zip in append mode
          zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
  
          # Write the file to the in-memory zip
          zf.writestr(filename_in_zip, file_contents)
  
          # Mark the files as having been created on Windows so that
          # Unix permissions are not inferred as 0000
          for zfile in zf.filelist:
              zfile.create_system = 0        
  
          return self
  
      def read(self):
          '''Returns a string with the contents of the in-memory zip.'''
          self.in_memory_zip.seek(0)
          return self.in_memory_zip.read()
  
      def writetofile(self, filename):
          '''Writes the in-memory zip to a file.'''
          f = file(filename, "w")
          f.write(self.read())
          f.close()
  </code></pre>