【代码】tarfile的使用

本文介绍了如何利用Python的tarfile模块进行流式压缩和解压操作,重点讨论了使用'w|'模式进行压缩以及'r|'模式进行解压的方法,这两种模式避免了额外的数据拷贝,实现了高效的数据传输。
摘要由CSDN通过智能技术生成

为了避免额外的数据拷贝,想了半天怎么压缩到socket,而且发现了issue

import tarfile
copybufsize = 1024
class Fo:
    def __init__(self):
        self.fileobj = open('myshm.tar', 'wb')
    def read(self,b):
        print('read')
        return self.fileobj.read(b)
    def tell(self):
        print('tell',self.fileobj.tell())
        return self.fileobj.tell()
    def write(self,write):
        r =self.fileobj.write(write)
        print('write',r)
        return r
    def close(self):
        print('Fo close')
        return self.fileobj.close()
fo = Fo()
t = tarfile.open('myshm.tar', 'w|', copybufsize=copybufsize,fileobj=fo,bufsize=copybufsize)
tarinfo = tarfile.TarInfo('aaashm')
tarinfo.size = 1 * 1024 ** 2


class Fi:
    def __init__(self):
        self.f = open('myshm', 'rb')

    def read(self, bs):
        print('read src',bs)
        return self.f.read(bs)

    def close(self):
        print('Fi close')

f = Fi()
t.addfile(tarinfo,)
t.close()
t.fileobj.close()
fo.close()
f.close()

原来打开模式就可以支持。。。
'w|'方式打开是stream方式的压缩写
'r|'方式打开是stream方式的解压读

    def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs):
        """Open a tar archive for reading, writing or appending. Return
           an appropriate TarFile class.

           mode:
           'r' or 'r:*' open for reading with transparent compression
           'r:'         open for reading exclusively uncompressed
           'r:gz'       open for reading with gzip compression
           'r:bz2'      open for reading with bzip2 compression
           'r:xz'       open for reading with lzma compression
           'a' or 'a:'  open for appending, creating the file if necessary
           'w' or 'w:'  open for writing without compression
           'w:gz'       open for writing with gzip compression
           'w:bz2'      open for writing with bzip2 compression
           'w:xz'       open for writing with lzma compression

           'x' or 'x:'  create a tarfile exclusively without compression, raise
                        an exception if the file is already created
           'x:gz'       create a gzip compressed tarfile, raise an exception
                        if the file is already created
           'x:bz2'      create a bzip2 compressed tarfile, raise an exception
                        if the file is already created
           'x:xz'       create an lzma compressed tarfile, raise an exception
                        if the file is already created

           'r|*'        open a stream of tar blocks with transparent compression
           'r|'         open an uncompressed stream of tar blocks for reading
           'r|gz'       open a gzip compressed stream of tar blocks
           'r|bz2'      open a bzip2 compressed stream of tar blocks
           'r|xz'       open an lzma compressed stream of tar blocks
           'w|'         open an uncompressed stream for writing
           'w|gz'       open a gzip compressed stream for writing
           'w|bz2'      open a bzip2 compressed stream for writing
           'w|xz'       open an lzma compressed stream for writing
        """
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值