Validating a zip file coming from stdin
Question: After some frustration with `unzip(1L)`, I've been trying to create a script
that will unzip and print out raw data from all of the files inside a zip
archive that is coming from stdin. I currently have the following, which
works:

    
    
    import sys, zipfile, StringIO
    
    stdin = StringIO.StringIO(sys.stdin.read())
    zipselect = zipfile.ZipFile(stdin)
    
    filelist = zipselect.namelist()
    for filename in filelist:
        print filename, ':' 
        print zipselect.read(filename)
    

When I try to add validation to check if it truly is a zip file, however, it
doesn't like it.

    
    
    ...
    
    zipcheck = zipfile.is_zipfile(zipselect)
    if zipcheck is not None:
        print 'Input is not a zip file.'
        sys.exit(1)
    
    ...
    

results in

    
    
    File "/home/chris/simple/zipcat/zipcat.py", line 13, in <module>
      zipcheck = zipfile.is_zipfile(zipselect)
    File "/usr/lib/python2.7/zipfile.py", line 149, in is_zipfile
      result = _check_zipfile(fp=filename)
    File "/usr/lib/python2.7/zipfile.py", line 135, in _check_zipfile
      if _EndRecData(fp):
    File "/usr/lib/python2.7/zipfile.py", line 203, in _EndRecData
      fpin.seek(0, 2)
    AttributeError: ZipFile instance has no attribute 'seek'
    

I assume it can't seek because it is not a file, as such?

Sorry if this is obvious, this is my first 'go' with Python.


Answer: You should pass `stdin` to `is_zipfile`, not `zipselect`. `is_zipfile` takes a
path to a file or a file object, not a `ZipFile`.

See [the zipfile.is_zipfile
documentation](http://docs.python.org/library/zipfile.html#zipfile.is_zipfile)

You are correct that a `ZipFile` can't seek because it isn't a file. It's an
archive, so it can contain many files.

