Skip to content

Instantly share code, notes, and snippets.

@ReneNyffenegger
Created September 6, 2014 06:23
Show Gist options
  • Save ReneNyffenegger/20d2ed058d86bdfaeae6 to your computer and use it in GitHub Desktop.
Save ReneNyffenegger/20d2ed058d86bdfaeae6 to your computer and use it in GitHub Desktop.
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