xylz,imxylz

关注后端架构、中间件、分布式和并发编程

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

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)
    
    f_in 
= zipfile.ZipFile(zip_file, 'r')
    infoes 
= None
    
if '*' == files:
        infoes 
= f_in.infolist()
    
else:
        infoes 
= []
        
for f in files:
            infoes.append(f_in.getinfo(f))
    
print "filename:\t\tuncompress\tcompress\tpercent\tcreatetime"
    
print "-----------------------------------------"
    total_usize,total_csize 
= (0,0)
    
for info in infoes:
        (year, month, day, hour, minute, second) 
= info.date_time
        dtime 
= datetime.datetime(year, month, day, hour, minute, second)
        filename,usize,csize 
= 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())
        total_usize 
+= usize
        total_csize 
+= csize
    f_in.close()

    
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.exit(0)
    zip_file 
= sys.argv[1]
    files 
= '*'
    
if len(sys.argv)>2:
        files 
= []
        
for f in sys.argv[2:]:
            files.append(f)
    list(zip_file,files)


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

只有注册用户登录后才能发表评论。


网站导航:
 

©2009-2014 IMXYLZ