首页 > 解决方案 > 如何使用 Powershell(压缩存档)在新文件夹中压缩存档?

问题描述

我需要在新文件夹中创建一个 zip 文件,但 powershellcompress-archive destinationpath不会强制创建文件夹。

我的目标在一个变量中。

$Composant = "foo.bar/foo.bar.ImportMe"

我的powershell代码是:

compress-archive -Path $outdirbuild/$Composant/* -DestinationPath "temp/OutilDeploiement/$($Composant).zip" -compressionlevel optimal -verbose

我的错误是:

Abandon :  The path 'temp\OutilDeploiement\foo.bar' either does not exist or is not a valid file system path.

标签: powershellzip

解决方案


这应该有效,如果没有,请告诉我:

$source = Join-Path $outdirbuild -ChildPath $Composant\*
$destination = 'absolutepath\to\zipfolder'
$fileName = 'zipfile.zip'

if(-not(Test-Path $destination))
{
    New-Item -Path $destination -ItemType Directory -Force > $null
}

$params = @{
    Path = $source
    DestinationPath = Join-Path $destination -ChildPath $fileName
    CompressionLevel = 'Optimal'
    Verbose = $true
}

Compress-Archive @params
  • 您可以在不确认的情况下添加-Force到压缩。
  • 在源路径的末尾添加\*您没有压缩根文件夹,只是确认这是否是您想要的。

推荐阅读