![](/img/trans.png)
[英]Is there way to chain tarfile generation and gnupg encryption in python?
[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.