Powershell:如何在使用函数创建 zip 时包含根目录

问题描述 投票:0回答:1

代码:

Add-Type -Assembly System.IO.Compression.FileSystem

function Compress-CompatibleArchive {

    param (
        [String[]] $Path,
        [String] $DestinationPath
    )

    $zipfilename = $DestinationPath
    $sourcedir = $Path

    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,$zipfilename, $compressionLevel, $false)
}

Export-ModuleMember -Function Compress-CompatibleArchive

执行命令:

.\PublishPlugin.ps1 C:\Users\USER\OneDrive\Pictures\testing C:\Users\USER\OneDrive\Pictures\zipdest\1.zip

O/P:这只压缩测试文件夹中的内容。

我想要的是包含测试文件夹和内容。

我们将非常感谢您的帮助。

powershell
1个回答
0
投票

如果您查看[System.IO.Compression.ZipFile]::CreateFromDirectory

的文档
,您将看到第四个参数称为
includeBaseDirectory
,将其设置为
$true
,它将包含存档中的目录。

[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,$zipfilename, $compressionLevel, $false)

成为

[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,$zipfilename, $compressionLevel, $true)
© www.soinside.com 2019 - 2024. All rights reserved.