Python: Unzipping and decompressing .Z files inside .zip
Question: I am trying to unzip a Alpha.zip folder which contains a Beta directory which
contains a Gamma Folder which contains a.Z, b.Z, c.Z, d.Z files. Using zip and
7-zip I was able to extract all a.D, b.D, c.D, d.D files stored within the .Z
files.

I tried this in python using Import gzip and Import zlib.

    
    
    import sys
    import os
    import getopt
    import gzip
    f = open('a.d.Z','r')
    file_content = f.read()
    f.close()
    

I keep getting all sorts of errors including: this is not a zip file, return
codecs.charmap_encode(input self.errors encoding_map) 0. Any suggestions as to
how to code this?


Answer: You need to actually make use of a zip library of some kind. Right now you're
importing `gzip`, but you're not doing anything with it. Try taking a look at
the [`gzip` documentation](http://docs.python.org/2/library/gzip) and opening
the file using that library.

    
    
    gzip_file = gzip.open('a.d.Z') # use gzip.open instead of builtin open function
    file_content = gzip_file.read()
    

Edit based on your comment: you can't just open all kinds of compressed files
with any compression library. Since you have a `.Z` file, it's likely that you
want to use [`zlib`](http://docs.python.org/2/library/zlib.html) rather than
`gzip`, but since extensions are just conventions, only you know for sure what
compression format your file is in. To use `zlib`, do something like this
instead:

    
    
    # Note: untested code ahead!
    import zlib
    with open('a.d.Z', 'rb') as f: # Notice that I open this in binary mode
        file_content = f.read() # Read the compressed binary data
        decompressed_content = zlib.decompress(file_content) # Decompress
    

