# -*- coding: UTF-8 -*-
'''
Created on 2011-6-9
@author: Administrator
'''
import os, zipfile
from os.path import join
import string
def zipfolder(foldername, filename, includeEmptyDIr=True):
os.chdir(foldername)
print foldername
empty_dirs = []
zip = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(foldername):
print root
startpath = root.replace(foldername, "")
print startpath
empty_dirs.extend([dir for dir in dirs if os.listdir(join(root, dir)) == []])
for name in files:
tmp = join(startpath ,name)
print tmp
zip.write(join(startpath ,name))
if includeEmptyDIr:
for dir in empty_dirs:
zif = zipfile.ZipInfo(join(startpath, dir) + os.sep)
zip.writestr(zif, "")
empty_dirs = []
zip.close()
def zipDir():
#sourceFold = 'D:\\www\\js'
sourceFold = 'D:\\360Downloads\\pxto_com_cn\\site\\'
targetFile = 'D:\\opt\\public\\technology\\test.zip'
zipfolder(sourceFold, targetFile)
def pathtest():
path = 'D:\\360Downloads\\pxto_com_cn\\site\\admin\\fuck'
print path.replace("D:\\360Downloads\\pxto_com_cn\\site\\", "")
print path
if __name__ == '__main__':
zipDir()
|