六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 54|回复: 0

python 压缩文件(3)

[复制链接]

升级  70.67%

48

主题

48

主题

48

主题

秀才

Rank: 2

积分
156
 楼主| 发表于 2013-1-15 02:21:33 | 显示全部楼层 |阅读模式
原来制成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[1]
        dstpath = temp[0]
    #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 名字不用,于是就搞一个改名的逻辑在里面,具体代码省略。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表