1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #!/usr/bin/python # -*- coding: UTF-8 -*-
from xml.dom.minidom import parse import xml.dom.minidom import json import os import time import sys import codecs import cgi import HTMLParser import re import base64 import zipfile
def zip_ya(startdir,file_news,basedir): # zip压缩文件 z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #参数一:文件夹名 for dirpath, dirnames, filenames in os.walk(startdir): print("dirpath:" + dirpath) fpath = dirpath.replace(startdir,'') #这一句很重要,不replace的话,就从根目录开始复制 print("fpath:" + fpath) fpath = fpath and fpath + os.sep or ''#实现当前文件夹以及包含的所有文件的压缩 print("fpath:" + fpath) for filename in filenames: print("filename:" + filename) z.write(os.path.join(dirpath, filename),basedir+os.sep+fpath+filename) # basedir 为zip中的目录 print ('zip success!') z.close()
|