Search code examples
pythonzipgzipsftpparamiko

How do I read a gzipped zip file on an sFTP server?


On an sFTP server I have gzipped files, each containing one zip file, which in turn do contain multiple files. What I want to retrieve is a complete list of files contained in the zipfiles. This is what I have put together so far by googling, but it doesn't seem to do anything. The gziplist works though.

Any ideas on what I'm doing wrong, or better yet - a better approach?

ssh_stdin, ssh_stdout, ssh_stderr = conn.exec_command(f'ls /{client}/Data/ | grep ".ZIP.gz"\n', get_pty=True)
gziplist = []
for i in ssh_stdout.read().decode("utf-8").split('\r\n'):
    gziplist.append(i)
    ssh_stdin, ssh_stdout, ssh_stderr = conn.exec_command(f'zless /{client}/Data/{i}\n', get_pty=True)
    gzcontent = ''
    for line in ssh_stdout.readlines():
        gzcontent = gzcontent+line
        gzfile = gzip.open(gzcontent)
        content = gzfile.read()
        contentbytes = zipfile.ZipFile(io.BytesIO(content))
        print(contentbytes.namelist())


Solution

  • As Martin suggested, I removed the get_pty=True, and that solved the issue. Had to adapt the rest of my code too because I made some more mistakes in the lines after it, as usual.

    for i in ssh_stdout.read().decode("utf-8").split('\r\n'):
        gziplist.append(i)
        ssh_stdin2, ssh_stdout2, ssh_stderr2 = conn.exec_command(f'zless {client}/Data/{i}\n')
        content = ssh_stdout2.read()
        contentbytes = zipfile.ZipFile(io.BytesIO(content))
        print(contentbytes.namelist())