Search code examples
pythonpython-2.7zipweb-widget

Check a .zip archive integrity/filepaths/compression methods from python?


I need to verify if an widget which basically is a .zip are according to this compliance rules:
http://www.w3.org/TR/widgets/#zip-archive

So what I need is to be able to check in that archive:

  • Compression Methods;
  • Version of Zip Needed to Extract a File Entry;
  • Zip Relative Paths (path lengths, character encoding, filenames)

What would be the way to approach this from python(what lib to use, some minimal code example would help)?


Solution

  • you should use the zipfile lib - try this http://bip.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/zipfile/index.html:

    To test if the file is a zip file:

    import zipfile
    
    for filename in [ 'README.txt', 'example.zip', 
                      'bad_example.zip', 'notthere.zip' ]:
        print '%20s  %s' % (filename, zipfile.is_zipfile(filename))
    

    And to access the info of a zip file:

    import datetime
    import zipfile
    
    def print_info(archive_name):
        zf = zipfile.ZipFile(archive_name)
        for info in zf.infolist():
            print info.filename
            print '\tComment:\t', info.comment
            print '\tModified:\t', datetime.datetime(*info.date_time)
            print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)'
            print '\tZIP version:\t', info.create_version
            print '\tCompressed:\t', info.compress_size, 'bytes'
            print '\tUncompressed:\t', info.file_size, 'bytes'
            print
    
    if __name__ == '__main__':
        print_info('example.zip')