問題描述
我想檢索存儲在 FTP 服務器上的壓縮 gz 文件中的數據,而不將文件寫入本地存檔.
I would like to retrieve the data inside a compressed gz file stored on an FTP server, without writing the file to the local archive.
目前我已經完成了
from ftplib import FTP
import gzip
ftp = FTP('ftp.server.com')
ftp.login()
ftp.cwd('/a/folder/')
fileName = 'aFile.gz'
localfile = open(fileName,'wb')
ftp.retrbinary('RETR '+fileName, localfile.write, 1024)
f = gzip.open(localfile,'rb')
data = f.read()
然而,這會將文件localfile"寫入當前存儲.
This, however, writes the file "localfile" on the current storage.
我試圖改變這個
from ftplib import FTP
import zlib
ftp = FTP('ftp.server.com')
ftp.login()
ftp.cwd('/a/folder/')
fileName = 'aFile.gz'
data = ftp.retrbinary('RETR '+fileName, zlib.decompress, 1024)
但是,ftp.retrbinary
不輸出其回調的輸出.有沒有辦法做到這一點?
but, ftp.retrbinary
does not output the output of its callback.
Is there a way to do this?
推薦答案
一個簡單的實現是:
將文件下載到內存中類似文件的對象,例如
BytesIO
;
將其傳遞給 fileobj 參數noreferrer">GzipFile
構造函數.
pass that to fileobj
parameter of GzipFile
constructor.
import gzip
from io import BytesIO
import shutil
from ftplib import FTP
ftp = FTP('ftp.example.com')
ftp.login('username', 'password')
flo = BytesIO()
ftp.retrbinary('RETR /remote/path/archive.tar.gz', flo.write)
flo.seek(0)
with open('archive.tar', 'wb') as fout, gzip.GzipFile(fileobj = flo) as gzip:
shutil.copyfileobj(gzip, fout)
<小時>
以上將整個 .gz 文件加載到內存中.對于大文件來說什么是低效的.更智能的實現將改為流式傳輸數據.但這可能需要實現一個智能的自定義類文件對象.
The above loads whole .gz file to a memory. What can be inefficient for large files. A smarter implementation would stream the data instead. But that would probably require implementing a smart custom file-like object.
另請參閱在 FTP 服務器上的 zip 文件中獲取文件名,而無需下載整個存檔.
這篇關于從 FTP 服務器上的 gz 文件中檢索數據而不在本地寫入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!