Create a gist now

Instantly share code, notes, and snippets.

ftp download gz file and unzip it in one go
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment