How To/python/MyMiscUtils/

# -*- coding: utf-8 -*-
 
import zipfile, os
 
class ZIP():
    def __init__(self, tree, zipname):
        # cfg
        self.zip = zipfile.ZipFile(zipname, 'w')
        self.rootlen = len(tree) + 1
        self.root = tree
        # /cfg
 
        self.__readtree(tree)
 
    def __readtree(self, path):
        for f in os.listdir(path):
            if os.path.isdir(os.path.join(path, f)):
                self.__readtree(os.path.join(path, f))
            else:
                self.zippath(os.path.join(path, f))
 
    def zippath(self, path):
        cwd = os.getcwd()
        os.chdir(self.root)
        self.zip.write(path[self.rootlen:])
        os.chdir(cwd)