#!/usr/bin/env python # -*- coding: utf-8 -*- # 开始压缩数据 import os import os.path import sys import zlib def compress_md5files(srcPath): files = os.listdir(srcPath) for f in files: path = os.path.join(srcPath, f) postfix = os.path.splitext(path)[1].lower() if (os.path.isdir(path)): compress_md5files(path) else: if os.path.isfile(path) and (postfix == ".m5m" or postfix == ".m5a"): compress_md5file(path) def compress_md5file(filePath): f = open(filePath, "r") rawData = f.read() f.close() cData = zlib.compress(rawData, zlib.Z_BEST_COMPRESSION) f = open(filePath + "c", "w") f.write(cData) f.close() print("压缩文件完毕:{0}".format(filePath)) if __name__ == "__main__": workingDir = "./" if 2 == len(sys.argv): srcPath = os.path.join(workingDir, sys.argv[1]) if os.path.isdir(srcPath): compress_md5files(srcPath) else: compress_md5file(srcPath) exit(0)