xylz,imxylz

          關(guān)注后端架構(gòu)、中間件、分布式和并發(fā)編程

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            111 隨筆 :: 10 文章 :: 2680 評(píng)論 :: 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 閱讀(16886) 評(píng)論(0)  編輯  收藏 所屬分類: Python

          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 大方县| 黑龙江省| 万载县| 富平县| 城市| 高雄市| 绥阳县| 浮山县| 德令哈市| 永嘉县| 筠连县| 托里县| 固镇县| 兰考县| 红安县| 靖安县| 勃利县| 蒙城县| 山阴县| 沙河市| 大理市| 枝江市| 方正县| 托里县| 连州市| 绍兴市| 黄浦区| 镶黄旗| 吉木萨尔县| 营口市| 温泉县| 隆安县| 中阳县| 绵阳市| 沈阳市| 姜堰市| 申扎县| 陵水| 梅河口市| 崇阳县| 枝江市|