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

时间:2017-01-27 14:03:19

标签: powershell zip powershell-v2.0

我需要一些帮助,将我的想法放在一个有效的代码中。

这就是我所拥有的:

第1步:我将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"

现在,我想建立一个连接。我谷歌为Syntax找到了这个。不确定这是否会建立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下载将为每次运行创建一个$version名称为C:\myApp的新子文件夹。

完成此操作后,我还需要知道如何在C:\myApp\$Version\Client\<content of the zip file will be here>

下解压缩此client.zip文件

2 个答案:

答案 0 :(得分:3)

您可以使用Expand-Archive cmdlet。它们以Powershell版本5提供。不确定以前的版本。请参阅以下语法:

Expand-Archive $zipFile -DestinationPath $targetDir -Force

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

使用您的参数,它将如下所示:

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

答案 1 :(得分:1)

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

取决于您是否尝试压缩或解压缩。

相关问题