Zipping files/zipfile module

Thomas Thomas thomas at mindz-i.co.nz
Tue Aug 1 22:56:59 EDT 2006


Hi Stephen,

some code that I have been using for a similar purpose. 

def addFolderToZip(myZipFile,folder):
    folder = folder.encode('ascii') #convert path to ascii for ZipFile Method
    for file in glob.glob(folder+"/*"):
            if os.path.isfile(file):
                print file
                myZipFile.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
            elif os.path.isdir(file):
                addFolderToZip(myZipFile,file)
                
def createZipFile(filename,files,folders):
    curTime=strftime("__%Y_%m_%d", time.localtime())
    filename=filename+curTime;
    print filename
    zipFilename=utils.getFileName("files", filename+".zip")
    myZipFile = zipfile.ZipFile( zipFilename, "w" ) # Open the zip file for writing 
    for file in files:
        file = file.encode('ascii') #convert path to ascii for ZipFile Method
        if os.path.isfile(file):
            (filepath, filename) = os.path.split(file)
            myZipFile.write( file, filename, zipfile.ZIP_DEFLATED )
    
    for folder in  folders:   
        addFolderToZip(myZipFile,folder)  
    myZipFile.close()
    return (1,zipFilename)



(success,filename)=createZipFile(planName,files,folders);


hope it helps..

cheers
-----------------------------------------------------
Thomas Thomas
thomas at mindz-i.co.nz
Phone.  +64 7 855 8478
Fax.      +64 7 855 8871
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060802/f946b93f/attachment.html>


More information about the Python-list mailing list