说明(2017-12-25 10:43:20):

1. CZ写的压缩bin包代码,记下来以后好抄。

 # coding:utf-8
'''
Created on 2014年8月14日 @author: johnsoncheng
''' import zipfile, os IGNORE_POSTFIX_LIST = {".mp3", ".wav", ".jpg", ".JPG", ".wma", ".png", ".mp4", ".avi", ".wmv"} def zipFolder():
# srcFolder = r"E:\XZJYRootFolder\root\Math_Video"
# tarFile = r"E:\XZJYRootFolder\root\Data\Math_Video.zip"
rootFolder = r"D:\软件发布\压缩根目录"
#rootFolder = r"D:\XZJYRootFolder\root\Data1\res"
# 遍历所有文件夹
folderList = os.listdir(rootFolder)
for folder in folderList:
folderPath = os.path.join(rootFolder, folder)
if os.path.isdir(folderPath):
# 取得二级目录
folderList1 = os.listdir(folderPath)
for folder1 in folderList1:
folderPath1 = os.path.join(folderPath, folder1)
if os.path.isdir(folderPath1):
zipPath = os.path.join(folderPath, folder1 + ".bin")
with zipfile.ZipFile(zipPath, 'w', allowZip64=True) as myzip:
zipSubFolder(myzip, folderPath1, folderPath)
pass def zipSubFolder(zipObj, folderPath, rootFolderPath):
global IGNORE_POSTFIX_LIST
print("compressing " + folderPath)
# 取得当前文件夹差异路径
folderRelativePath = folderPath[len(rootFolderPath): ]
# 插入当前文件夹
zipObj.write(folderPath, folderRelativePath)
# 遍历所有子实体
entryList = os.listdir(folderPath)
# 处理所有子文件夹
for entry in entryList:
entryPath = os.path.join(folderPath, entry)
if os.path.isdir(entryPath):
zipSubFolder(zipObj, entryPath, rootFolderPath)
# 处理子文件
for entry in entryList:
entryPath = os.path.join(folderPath, entry)
if os.path.isfile(entryPath):
fileRelativePath = folderPath[len(rootFolderPath): ] + "/" + entry
tempRoot, tempPostFix = os.path.splitext(fileRelativePath)
if tempPostFix in IGNORE_POSTFIX_LIST:
zipObj.write(entryPath, fileRelativePath)
else:
zipObj.write(entryPath, fileRelativePath, zipfile.ZIP_DEFLATED)
pass if __name__ == '__main__':
print("************** start *************")
zipFolder()
print("************** end *************")
pass
05-11 15:14