六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 41|回复: 0

python 压缩文件(2)

[复制链接]

升级  70.67%

48

主题

48

主题

48

主题

秀才

Rank: 2

积分
156
 楼主| 发表于 2013-1-15 02:21:35 | 显示全部楼层 |阅读模式
加入tar中间过程:

<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;"># -*- 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:\\AutoUpdate\\DataDist\\viruswall\\Data\\KSVW-VirusDB\\","d:\\AutoUpdate\\DataDist\\viruswall\\Data\\update\\KSVW-VirusDB.tgz")
    
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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