关闭

python zipfile

289人阅读 评论(0) 收藏 举报
import zipfile,os
#-coding:utf8
'''
https://docs.python.org/3/library/zipfile.html
'''

'''
压缩目录.zip
'''
#
def zip_dir(dirname,zipfilename):

    with zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED) as zf:

        for root,dirs,files in os.walk(dirname):
            for filename in files:
                print("file:"+os.path.join(root,filename))
                zf.write(os.path.join(root,filename))
            if len(files) == 0:
                print("empty dir.")
                zif = zipfile.ZipInfo(root+'/')
                zf.writestr(zif,'')

'''
解压到目录会覆盖同名的文件(增量解压
'''
def archives():
    zip = os.path.join('/opt/install','test.zip')
    zip_ref = zipfile.ZipFile(zip, 'r')
    zip_ref.extractall('/opt/www/webapps')
    zip_ref.close()
if __name__=='__main__':
    zip_dir('tests','tests.zip')
    #archives()

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82419次
    • 积分:1621
    • 等级:
    • 排名:千里之外
    • 原创:70篇
    • 转载:82篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论