pipe/stream gnupg 输出到 tarfile 中
pipe/stream gnupg output to tarfile in
我有以下代码,但显然这不是真正的流媒体。这是我能找到的最好的,但它首先将整个输入文件读入内存。我想在解密大文件(>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()
显然,您不能使用 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()
我有以下代码,但显然这不是真正的流媒体。这是我能找到的最好的,但它首先将整个输入文件读入内存。我想在解密大文件(>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()
显然,您不能使用 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()