:: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 10 文章 :: 2680 评论 :: 0 Trackbacks

ZIP Archive Files. The unzip -l command to dump the contents of ZIP archive is boring. Create a Python script called lszip.py that gives additional information such as: the compressed file size, the compressed percentage of each file (by comparing the original and compressed file sizes), and a full time.ctime() timestamp instead of the unzip output (of just the date and HH:MM). Hint: The date_time attribute of an archived file does not contain enough information to feed to time.mktime()... it is up to you!

#!/usr/bin/env python
-*- coding:utf-8 -*-
$Id: p0922.py 168 2010-06-29 06:34:01Z xylz $

This is a 'python' study plan for xylz.
Copyright (C)2010 xylz (www.imxylz.info)

import zipfile
import os
import datetime

def list (zip_file,files):
print "list %s files from %s" % (len(files) if files!='*' else 'total',zip_file)
= zipfile.ZipFile(zip_file, 'r')
= None
if '*' == files:
= f_in.infolist()
= []
for f in files:
print "filename:\t\tuncompress\tcompress\tpercent\tcreatetime"
print "-----------------------------------------"
= (0,0)
for info in infoes:
        (year, month, day, hour, minute, second) 
= info.date_time
= datetime.datetime(year, month, day, hour, minute, second)
= info.filename,info.file_size,info.compress_size 
print "%s:\t\t%d\t%d\t%d%%\t%s" % (filename,usize,csize,(csize*100/(usize+0.01)),dtime.ctime())
+= usize
+= csize

print "-----------------------------------------"
print "compressed size %d bytes, uncompressed size %d bytes, %d%%" % (total_csize,total_usize,(total_csize*100/total_usize))

if __name__ == '__main__':
import sys
if len(sys.argv)<2:
print "List files in zip file"
print "Usage: %s <zipfile> [files]" % (sys.argv[0],)
= sys.argv[1]
= '*'
if len(sys.argv)>2:
= []
for f in sys.argv[2:]:

©2009-2014 IMXYLZ |求贤若渴
posted on 2010-06-29 14:56 imxylz 阅读(16778) 评论(0)  编辑  收藏 所属分类: Python



©2009-2014 IMXYLZ