import zlib
from ftplib import FTP
def get_gz(ftp, ftp_filename, local_filename):
decomp = zlib.decompressobj(16+zlib.MAX_WBITS)
unzip = open (local_filename, 'wb')
def next_packet(data):
unzip.write(decomp.decompress(data))
ftp.retrbinary('RETR ' + ftp_filename, next_packet)
decompressed = decomp.flush()
unzip.write(decompressed)
unzip.close()
ftp_ = FTP('ftp.host.xyz')
ftp_.login()
ftp_.cwd('/foo/bar')
get_gz(ftp_, 'remote-file.gz', 'local-file')
Link to github gist
No comments:
Post a Comment