python 压缩文件的代码

发布时间:2019-09-20编辑:脚本学堂
python 压缩文件
先来看最简单的压缩文件的方法:
#  -*- coding:utf-8 -*-
# =================================================
#
#    brief   :    制作viruswall 的离线升级数据
#
#    author  :    hechangmin
#
#    date    :    2008.6 
#
# ==================================================

import  os
import  threading, zipfile

class  AsyncZip(threading.Thread):
    
     def   __init__ (self, infile, outfile):
        threading.Thread. __init__ (self)        
        self.infile  =  infile
        self.outfile  =  outfile
     def  run(self):
        state  =   ' w '
         if  os.path.isfile(self.outfile)  ==  True:
            state  =   ' a '
        f  =  zipfile.ZipFile(self.outfile, state, zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
         print   ' Finished background zip of:  ' , self.infile
        
def  zipDir(src,dst):
    
    initPath  =  os.getcwd()
    
    tempDST  =   os.path.join(os.getcwd(),dst)
    tempSRC  =   os.path.join(os.getcwd(),src)
    os.chdir( tempSRC )
    files  =  os.listdir(os.curdir)
     print  files
     for  file  in  files:
        background  =  AsyncZip(file,dst)
        background.start()
        background.join()     #  Wait for the background task to finish
        
    os.chdir( initPath )
     print  os.getcwd()
# test ok
if   __name__   ==   ' __main__ ' :
    
    zipDir( " D:AutoUpdateDataDistviruswallDataKSVW-VirusDB " , " d:AutoUpdateDataDistviruswallDataupdateKSVW-VirusDB.tgz " )

显然这里压缩成功了,但是压缩采用的是zip算法。
还希望是采用 tar+ gzip 的方式。

加入tar中间过程:

#  -*- coding:utf-8 -*-
# ===============================================
#
#    brief   :    制作viruswall 的离线升级数据
#
#    author  :    hechangmin
#
#    date    :    2008.6 
#
#    notice  :    先变成tar 文件
# ===============================================

import  os
import  threading, zipfile
import  tarfile

class  AsyncZip(threading.Thread):
    
     def   __init__ (self, infile, outfile):
        threading.Thread. __init__ (self)        
        self.infile  =  infile
        self.outfile  =  outfile
     def  run(self):
        state  =   ' w '
         if  os.path.isfile(self.outfile)  ==  True:
            state  =   ' a '
        f  =  zipfile.ZipFile(self.outfile, state, zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
         print   ' Finished background zip of:  ' , self.infile
        
def  zipDir(src,dst):
    
    initPath  =  os.getcwd()
    
    tempDST  =   os.path.join(os.getcwd(),dst)
    tempSRC  =   os.path.join(os.getcwd(),src)
    os.chdir( tempSRC )
    files  =  os.listdir(os.curdir)
    tar  =  tarfile.open( " temp.tar " , " w " )
     for  file  in  files:
        tar.add(file)
    tar.close()
    background  =  AsyncZip( " temp.tar " ,dst)
    background.start()
    background.join()     #  Wait for the background task to finish
        
    os.chdir( initPath )
     print  os.getcwd()
# test ok
if   __name__   ==   ' __main__ ' :
    
    zipDir( " D:AutoUpdateDataDistviruswallDataKSVW-VirusDB " , " d:AutoUpdateDataDistviruswallDataupdateKSVW-VirusDB.tgz " )

当然还不够,虽然加入了tar环节,但是最后还是zip压缩。

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:AutoUpdateDataDistviruswallDataKSVW-VirusDB","d:AutoUpdateDataDistviruswallDataupdateKSVW-VirusDB.tgz")

#当然我发现里面的tar 名字不用,于是就搞一个改名的逻辑在里面,具体代码省略。