简体   繁体   English

如何在 sFTP 服务器上读取压缩后的 zip 文件?

[英]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.在 sFTP 服务器上,我有 gzip 文件,每个文件包含一个 zip 文件,而该文件又包含多个文件。 What I want to retrieve is a complete list of files contained in the zipfiles.我想要检索的是 zip 文件中包含的文件的完整列表。 This is what I have put together so far by googling, but it doesn't seem to do anything.到目前为止,这是我通过谷歌搜索整理的内容,但它似乎没有做任何事情。 The gziplist works though. gziplist虽然有效。

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())

As Martin suggested, I removed the get_pty=True, and that solved the issue.正如 Martin 所建议的,我删除了 get_pty=True,这解决了问题。 Had to adapt the rest of my code too because I made some more mistakes in the lines after it, as usual.也必须调整我的代码的 rest,因为我像往常一样在它后面的行中犯了更多错误。

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM