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 閱讀(16885) 評論(0)  編輯  收藏 所屬分類: Python

          ©2009-2014 IMXYLZ
          主站蜘蛛池模板: 乌拉特前旗| 永昌县| 金阳县| 榆树市| 安达市| 岚皋县| 南阳市| 乐业县| 皮山县| 恩平市| 新河县| 垦利县| 惠安县| 铜鼓县| 奎屯市| 惠水县| 霍林郭勒市| 永泰县| 高平市| 西乡县| 松潘县| 上饶县| 泸水县| 泸州市| 邓州市| 满城县| 曲沃县| 乐昌市| 锦州市| 宁陵县| 遵义市| 孝义市| 三明市| 巴青县| 乌鲁木齐县| 舞钢市| 彭州市| 香河县| 阜城县| 凤阳县| 重庆市|