【问题标题】:pipe/stream gnupg output to tarfile in管道/流 gnupg 输出到 tarfile 中
【发布时间】:2019-10-27 07:40:54
【问题描述】:

我有以下代码,但显然这不是真正的流媒体。这是我能找到的最好的,但它首先将整个输入文件读入内存。我想在解密大文件(>100Gb 文件)时将其流式传输到 tarfile 模块而不使用所有内存

import tarfile, gnupg                                                                                                                                                                                                                                
gpg = gnupg.GPG(gnupghome='C:/Users/niels/.gnupg')                                                                         

with open('103330-013.tar.gpg', 'r') as input_file:                                                                                                                                                                                                   
    decrypted_data = gpg.decrypt(input_file.read(), passphrase='aaa')                                                       
    # decrypted_data.data contains the data                                                                                 
    decrypted_stream = io.BytesIO(decrypted_data.data)                                                                      

    tar = tarfile.open(decrypted_stream, mode='r|')                                                                                                                                                                                                 
    tar.extractall()                                                                                                                                                                                                                                
    tar.close()

【问题讨论】:

    标签: python stream pipe tar


    【解决方案1】:

    显然,您不能使用 gpnupg 模块使用真正的流式传输,gnupg 模块总是将 gnupg 的整个输出读取到内存中。 所以要使用真正的流媒体,你必须直接运行 gpg 程序。 这是一个示例代码(没有适当的错误处理):

    import subprocess
    import tarfile
    
    with open('103330-013.tar.gpg', 'r') as input_file:
       gpg = subprocess.Popen(("gpg", "--decrypt", "--homedir", 'C:/Users/niels/.gnupg', '--passphrase', 'aaa'), stdin=input_file, stdout=subprocess.PIPE)
       tar = tarfile.open(fileobj=gpg.stdout, mode="r|")
       tar.extractall()
       tar.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 2013-07-23
      • 2010-09-26
      • 2018-02-21
      • 2011-05-12
      相关资源
      最近更新 更多