简体   繁体   English

使用 Python 在远程服务器上解压缩 zip 文件

[英]Unzip a zip file on remote server using Python

I need to write Python code to login to remote server and navigate to Zip file path and then unzip & save on remote server.我需要编写 Python 代码来登录到远程服务器并导航到 Zip 文件路径,然后解压缩并保存在远程服务器上。 As a next step, I need to access the files inside the unzipped folder.下一步,我需要访问解压缩文件夹中的文件。 Can anyone please help.任何人都可以请帮忙。 I referred few links but unable to get complete solution.我提到了几个链接,但无法获得完整的解决方案。 https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73 https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73

First you can upload file using .put() and next you can use external program unzip to uncompress it.首先你可以使用.put()上传文件,然后你可以使用外部程序unzip来解压缩它。

Something similar to类似的东西

import paramiko

# login
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='192.168.0.1', username='user', password='PaSsWoRd')

# upload
ftp_client = ssh_client.open_sftp()
ftp_client.put('local.zip', 'remote.zip')
ftp_client.close()

# unzip
stdin, stdout, stderr = ssh_client.exec_command('unzip remote.zip')
print(stdout.read().decode())

# access files
stdin, stdout, stderr = ssh_client.exec_command('ls')
print(stdout.read().decode())

If you want to put in subfolder then you may need to create this subfolder如果要放入子文件夹,则可能需要创建此子文件夹

stdin, stdout, stderr = ssh_client.exec_command('mkdir upload')

ftp_client.put('local.zip', 'upload/remote.zip')

stdin, stdout, stderr = ssh_client.exec_command('cd upload ; unzip remote.zip')

stdin, stdout, stderr = ssh_client.exec_command('ls upload')
# or 
stdin, stdout, stderr = ssh_client.exec_command('cd upload ; ls')

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

相关问题 如何使用python找到一个zip文件,解压缩它,在其中找到一个特定文件? - How to find a zip file, unzip it, find a specific file in it using python? 使用子进程 python 库使用 7zip 解压缩文件 - Use subprocess python library to unzip a file using 7zip 如何使用 Python 解压缩受密码保护的 zip 文件 - How do I unzip a password protected zip file using Python 用于在远程服务器中创建 zip 文件的 Python 脚本 - Python script for creating zip file in remote server 为什么 python 无法解压缩由 winrar 使用 zip 方法创建的受密码保护的 zip 文件? - why can't python unzip a password protected zip file created by winrar using the zip method? 无法使用 python zipfile 库使用密码解压缩 a.zip 文件 - Unable to unzip a .zip file with a password with python zipfile library 如何在 python 的 mac os 上解压缩.app.zip 文件? - How to unzip .app.zip file on mac os in python? Python shutil打包一个zip文件解压回来EOF错误 - Python shutil pack a zip file and unzip it back EOF error 如何解压缩zip文件并将其显示为python-Django中的列表 - How to unzip a zip file and show it as list in python-Django Python:将文件解压缩到当前工作目录,但不保存zip中的目录结构 - Python: Unzip a file to current working directory but not maintain directory structure in zip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM