PowerShell脚本:下载zip文件并解压缩

8

我希望你能帮助我将思路转化成可执行的代码。

以下是我的思路:

第一步:我将以参数形式获得FTP用户名和密码。

param(#define parameters
[Parameter(Position=0,Mandatory=$true)]
    [string]$FTPUser
[Parameter(Position=1,Mandatory=$true)]
    [string]$FTPPassword    
[Parameter(Position=2,Mandatory=$true)]
    [string]$Version    
)

然后我设置了这些变量:

$FTPServer = "ftp.servername.com"
$SetType = "bin"

现在,我想建立一个连接。我在谷歌上搜索了语法并找到了这个结果。不确定这是否会建立FTP连接。我还没有进行测试。

$webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPassword) 

这是我不知道如何编写的部分:

$Version 是我的一个输入参数。我在FTP上有一个zip文件,其路径为:

ftp.servername.com\builds\my builds\$Version\Client\Client.zip

我希望将Client.zip下载到本地计算机(运行脚本的位置)的“C:\ myApp \ $ Version”文件夹中。因此,FTP下载将在每次运行时在“C:\ myApp”中创建一个名为$version的新子文件夹。
完成后,我还需要知道如何在“C:\ myApp \ $ Version \ Client \ ”下解压缩此client.zip文件。

2
解压缩: https://dev59.com/u14c5IYBdhLWcg3wj7F6 或 https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.archive/expand-archive。我还建议查看WinSCP汇编程序的PowerShell https://winscp.net/eng/docs/library_powershell - dbso
2个回答

15
您可以使用Expand-Archive命令。它们在PowerShell版本5中可用,不确定在以前的版本中是否可用。请参见以下语法:

Expand-Archive $zipFile -DestinationPath $targetDir -Force

-Force参数将强制覆盖目标目录中的文件(如果存在)。

使用您的参数,它会像这样:

Expand-Archive "C:\myApp\$Version\Client.zip" -DestinationPath "C:\myApp\$Version" -Force


谢谢@alltej,它能打开https uri吗? - Sergei Krivonos

2
Add-Type -assembly "System.IO.Compression.Filesystem";
[String]$Source = #pathA ;
[String]$Destination = #pathB ;
[IO.Compression.Zipfile]::ExtractToDirectory($Source, $Destination);

或者

[IO.Compression.Zipfile]::CreateFromDirectory($Source,$Destination);

根据您是尝试压缩还是解压缩。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接