代码:
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:这只压缩测试文件夹中的内容。
我想要的是包含测试文件夹和内容。
我们将非常感谢您的帮助。
如果您查看[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)