zhouu 发表于 2017-4-30 13:10:05

python 压缩文件(3)

  原来制成tar+gzip ,还是很简单的:

# -*- coding:utf-8 -*-
#===================================================================
#
#   brief   :    制作viruswall 的离线升级数据
#
#   author  :    hechangmin
#
#   date    :    2008.6
#
#   notice  :    先变成tar 文件
#====================================================================
import os
import tarfile
import gzip
import string
import shutil
  def zipDir(src,dst):
   
    initPath = os.getcwd()
    #tempDST =  os.path.join(os.getcwd(),dst)
    #tempSRC =  os.path.join(os.getcwd(),src)
    os.chdir( src )
    files = os.listdir(src)
    if dst.find("\\") != -1:
        temp = dst.rsplit("\\",1)
        dstname = temp
        dstpath = temp
    #print files
    tar = tarfile.open(dstname,"gz")
    for file in files:
        tar.add(file)
    tar.close()
    os.chdir( initPath )
    if os.path.isfile(dst) == True:
        os.remove(dst)
    shutil.copy(os.path.join(src,dstname), dst)
    os.remove(os.path.join(src,dstname))
    print os.getcwd()
#test ok
if __name__ == '__main__':
   
    zipDir("D:\\AutoUpdate\\DataDist\\viruswall\\Data\\KSVW-VirusDB\\","d:\\AutoUpdate\\DataDist\\viruswall\\Data\\update\\KSVW-VirusDB.tgz")
   
#当然我发现里面的tar 名字不用,于是就搞一个改名的逻辑在里面,具体代码省略。
页: [1]
查看完整版本: python 压缩文件(3)