python压缩打包脚本


python压缩打包脚本

  • common压缩py
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()
  • 根据渠道压缩
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
30
31
32
33
34
35
#!/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

from compile_common import zip_ya

if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')

RootDir = os.getcwd()
print("RootDir:" + RootDir)

channel = 'center' # 渠道名
print("channel:" + channel)
startdir = RootDir + '/build/web-mobile/'#要压缩的文件夹路径
print("startdir:" + startdir)
basedir = 'web-mobile-' + channel#压缩包里的文件夹
print("basedir:" + basedir)
if __name__ == '__main__':
file_news = 'web-mobile-' + channel +'.zip' # 压缩后文件夹的名字
print("file_news:" + file_news)
zip_ya(startdir,file_news,basedir)