简体   繁体   English

从python检查.zip存档的完整性/文件路径/压缩方法?

[英]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: 我需要验证基本上是.zip的小部件是否符合此合规性规则:
http://www.w3.org/TR/widgets/#zip-archive 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版本;
  • 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)? 从python处理此问题的方法是什么(使用什么lib,一些最小的代码示例会有所帮助)?

you should use the zipfile lib - try this http://bip.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/zipfile/index.html : 您应该使用zipfile库-试试这个http://bip.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/zipfile/index.html

To test if the file is a zip file: 要测试该文件是否为zip文件,请执行以下操作:

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: 并访问zip文件的信息:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM