Attachment 'zfile.py'

Download

Toggle line numbers
   1 #   Programmer: limodou
   2 #   E-mail:     limodou@gmail.com
   3 #
   4 #   Copyleft 2005 limodou
   5 #
   6 #   Distributed under the terms of the GPL (GNU Public License)
   7 #
   8 #   ZFile is free software; you can redistribute it and/or modify
   9 #   it under the terms of the GNU General Public License as published by
  10 #   the Free Software Foundation; either version 2 of the License, or
  11 #   (at your option) any later version.
  12 #
  13 #   This program is distributed in the hope that it will be useful,
  14 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 #   GNU General Public License for more details.
  17 #
  18 #   You should have received a copy of the GNU General Public License
  19 #   along with this program; if not, write to the Free Software
  20 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21 #
  22 #   $Id$
  23 
  24 import zipfile
  25 import os
  26 import os.path
  27 import sys
  28 import getopt
  29 
  30 __appname__ = 'zfile'
  31 __author__ = 'limodou'
  32 __version__ = '0.1'
  33 
  34 class ZFile(object):
  35     def __init__(self, filename, mode='r', basedir='', visitor=None):
  36         self.filename = filename
  37         self.mode = mode
  38         if self.mode in ('w', 'a'):
  39             self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
  40         else:
  41             self.zfile = zipfile.ZipFile(filename, self.mode)
  42         self.basedir = basedir
  43         if not self.basedir:
  44             self.basedir = os.path.dirname(filename)
  45         self.basedir = os.path.normpath(os.path.abspath(self.basedir)).replace('\\', '/')
  46         self.visitor = visitor
  47         
  48     def addfile(self, path, arcname=None):
  49         path = os.path.normpath(path).replace('\\', '/')
  50         if not arcname:
  51             if path.startswith(self.basedir + '/'):
  52                 arcname = path[len(self.basedir) + 1:]
  53             else:
  54                 arcname = ''
  55         if self.visitor:
  56             self.visitor(path, arcname)
  57         self.zfile.write(path, arcname)
  58             
  59     def addfiles(self, paths):
  60         for path in paths:
  61             if isinstance(path, tuple):
  62                 p, arcname = path
  63                 p = os.path.abspath(p)
  64                 if os.path.isdir(p):
  65                     self.addpath(p)
  66                 else:
  67                     self.addfile(p, arcname)
  68             else:
  69                 path = os.path.abspath(path)
  70                 if os.path.isdir(path):
  71                     self.addpath(path)
  72                 else:
  73                     self.addfile(path)
  74                 
  75     def addpath(self, path):
  76         files = self._getallfiles(path)
  77         self.addfiles(files)
  78             
  79     def close(self):
  80         self.zfile.close()
  81         
  82     def extract_to(self, path):
  83         for p in self.zfile.namelist():
  84             self.extract(p, path)
  85             
  86     def extract(self, filename, path):
  87         if not filename.endswith('/'):
  88             f = os.path.join(path, filename)
  89             dir = os.path.dirname(f)
  90             if not os.path.exists(dir):
  91                 os.makedirs(dir)
  92             file(f, 'wb').write(self.zfile.read(filename))
  93 
  94     def _getallfiles(self, path):
  95         fset = []
  96         for root, dirs, files in os.walk(path):
  97             for f in files:
  98                 fset.append(os.path.join(root, f))
  99         return fset
 100             
 101         
 102 def create(zfile, files, basedir='', visitor=None):
 103     z = ZFile(zfile, 'w', basedir, visitor=visitor)
 104     z.addfiles(files)
 105     z.close()
 106     
 107 def extract(zfile, path):
 108     z = ZFile(zfile)
 109     z.extract_to(path)
 110     z.close()
 111    
 112 def main():
 113     #process command line
 114 
 115     cmdstring = "Vhl:i:b:"
 116 
 117     try:
 118         opts, args = getopt.getopt(sys.argv[1:], cmdstring, [])
 119     except getopt.GetoptError:
 120         Usage()
 121         sys.exit(1)
 122 
 123     filelist = ''
 124     inputfiles = ''
 125     basedir = ''
 126     for o, a in opts:
 127         if o == '-V':       #version
 128             Version()
 129             sys.exit()
 130         elif o == '-h':       
 131             Usage()
 132             sys.exit()
 133         elif o == '-l':
 134             filelist = a
 135         elif o == '-i':
 136             inputfiles = a
 137         elif o == '-b':
 138             basedir = a
 139 
 140     if not basedir:
 141         basedir = os.getcwd()
 142         
 143     if inputfiles and filelist or len(args) < 1:
 144         Usage()
 145         sys.exit()
 146         
 147     zipfile = args[0]
 148     if not inputfiles and len(args) > 1:
 149         fout = None
 150         if filelist:
 151             fout = file(filelist, 'w')
 152             
 153         def visitor(path, arcname, f=fout):
 154             if f:
 155                 f.write(arcname + '\n')
 156 
 157         if len(args) == 2 and os.path.isdir(args[1]):
 158             create(zipfile, args[1:], basedir, visitor)
 159         else:
 160             create(zipfile, args[1:], basedir, visitor=visitor)
 161         if fout:
 162             fout.close()
 163     elif inputfiles and len(args) == 1:
 164         lines = file(inputfiles).readlines()
 165         files = [f.strip() for f in lines]
 166         create(zipfile, files, basedir)
 167     
 168     else:
 169         Usage()
 170         sys.exit()
 171     
 172             
 173 def Usage():
 174     print """Usage %s [options] zipfilename directories|filenames
 175       %s -i listfile zipfilename
 176 
 177 -V      Show version
 178 -h      Show usage
 179 -l      Create a listinfo file
 180 -i      Input file list
 181 -b      Specify base directory
 182 """ % (sys.argv[0], sys.argv[0])
 183 
 184 def Version():
 185     print """%s Copyleft GPL
 186 Author: %s
 187 Version: %s""" % (__appname__, __author__, __version__)
 188 
 189 if __name__ == '__main__':
 190 #    a = ZFile('d:/aaaa.zip', 'w')
 191 #    a.addfile('d:/a.csv')
 192 #    a.addfile('d:/test/a.JPG')
 193 #    a.addfile('d:/test/mixin/main.py', 'main.py')
 194 #    files = ['d:/a.csv', 'd:/test/a.JPG', ('d:/test/mixin/main.py', 'main.py')]
 195 ##    a.addfiles(files)
 196 ##    a.close()        
 197 #    create('d:/aaaa.zip', files)
 198 #    a = ZFile('d:/aaaa.zip')
 199 #    a.extract_to('d:/test/aaaa')
 200 #    extract('d:/aaaa.zip', 'd:/test/aaaa/aaaa')
 201     main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-05-11 08:52:11, 6.4 KB) [[attachment:Casing.py]]
  • [get | view] (2021-05-11 08:52:11, 627.0 KB) [[attachment:Depends.exe]]
  • [get | view] (2021-05-11 08:52:11, 4.0 KB) [[attachment:OPML.py]]
  • [get | view] (2021-05-11 08:52:11, 1488.9 KB) [[attachment:UliPad.3.9.dmg]]
  • [get | view] (2021-05-11 08:52:11, 1972.5 KB) [[attachment:backblog.zip]]
  • [get | view] (2021-05-11 08:52:11, 5.7 KB) [[attachment:backblog_src.zip]]
  • [get | view] (2021-05-11 08:52:11, 26.1 KB) [[attachment:cngtalkbot.rar]]
  • [get | view] (2021-05-11 08:52:11, 17.0 KB) [[attachment:comm_dev.rar]]
  • [get | view] (2021-05-11 08:52:11, 3708.8 KB) [[attachment:confbot_1_32.zip]]
  • [get | view] (2021-05-11 08:52:11, 14.9 KB) [[attachment:django_woodlog.zip]]
  • [get | view] (2021-05-11 08:52:11, 19.0 KB) [[attachment:djangoinstall.tar.gz]]
  • [get | view] (2021-05-11 08:52:11, 894.1 KB) [[attachment:djangostepbystep.zip]]
  • [get | view] (2021-05-11 08:52:11, 324.4 KB) [[attachment:docbook_step.zip]]
  • [get | view] (2021-05-11 08:52:11, 56.5 KB) [[attachment:gtalkbot1_6b.rar]]
  • [get | view] (2021-05-11 08:52:11, 75.2 KB) [[attachment:gtalkbot1_7.rar]]
  • [get | view] (2021-05-11 08:52:11, 76.0 KB) [[attachment:gtalkbot1_8.rar]]
  • [get | view] (2021-05-11 08:52:11, 71.5 KB) [[attachment:gtalkbot1_9.rar]]
  • [get | view] (2021-05-11 08:52:11, 57.3 KB) [[attachment:gtalkbot1_9_1.zip]]
  • [get | view] (2021-05-11 08:52:11, 58.0 KB) [[attachment:gtalkbot1_9_2.zip]]
  • [get | view] (2021-05-11 08:52:11, 418.0 KB) [[attachment:limodou_blog_2004.zip]]
  • [get | view] (2021-05-11 08:52:11, 693.2 KB) [[attachment:limodou_blog_2005.zip]]
  • [get | view] (2021-05-11 08:52:11, 23.7 KB) [[attachment:liteprog.html]]
  • [get | view] (2021-05-11 08:52:11, 14.5 KB) [[attachment:liteprog.zip]]
  • [get | view] (2021-05-11 08:52:11, 126.8 KB) [[attachment:mod_python-3.1.4.win32-py2.4.exe]]
  • [get | view] (2021-05-11 08:52:11, 9.7 KB) [[attachment:newtest.zip]]
  • [get | view] (2021-05-11 08:52:11, 163.7 KB) [[attachment:python_2_4_tut.rar]]
  • [get | view] (2021-05-11 08:52:11, 156.1 KB) [[attachment:pythontut.rar]]
  • [get | view] (2021-05-11 08:52:11, 1546.7 KB) [[attachment:svn-win32-1.2.0_py24.rar]]
  • [get | view] (2021-05-11 08:52:11, 6844.1 KB) [[attachment:wxPython2.7-win32-unicode-2.7.1.1-py24.exe]]
  • [get | view] (2021-05-11 08:52:11, 5.7 KB) [[attachment:zfile.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.