我是Powershell的新手。 我有一个需要,我认为这将是完美的。 不过,我很快就到了我觉得我头脑发昏的地步。 基本上,我需要创build一个.zip文件。
我有一个如下所示的目录:
MyStuff Dir1 Child1 File1.txt Child2 File1.png Child3 File1.txt Dir2 File1.txt File1.txt File2.txt File3.txt ...
我需要创build一个名为bundle.zip的.zip文件。 Bundle.zip不应该包含MyStuff中的所有文件。 相反,我需要在.zip文件中包含MyStuff / File1.txt,MyStuff / File2.txt,MyStuff / File3.txt和MyStuff / Dir1 / *。 我不知道如何用PowerShell做到这一点。
我的机器上安装了PowerShell v1.0。 我一直在尝试PowerShell社区扩展的“Write-Zip”,但是,我得到一个错误:“Write-Zip”一词不被识别为cmdlet,函数,脚本文件或可操作的名称程序”。
我究竟做错了什么?
您需要下载与安装的Powershell版本兼容的Powershell Community Extension并安装它。 安装完成后您需要将PSCX模块从程序文件位置移动到Powershell模块,然后尝试使用Write-Zip命令。
可以使用.NET类System.IO.Compression.ZipFile的CreateFromDirectory方法完成压缩。 我更喜欢使用.NET来安装社区扩展和其他附加组件,以便尽可能保持服务器环境的清洁。
我相信你不能用它来添加到一个zip压缩文件,但是需要临时复制正确结构的文件和文件夹到一个登台目录。 从那里你可以创buildzip文件并删除分段内容。
要使用.NET进行压缩,首先需要加载类组件:
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
然后你可以打电话给class级:
[System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir)
哪里:
$DirPath是你想压缩的目录。
$ZipFilePath是您要创build的zipfile的名称。
$Compressionlevel可以设置为Optimal , Fastest或NoCompression 。
$includeBaseDir可以是$true或$false 。
我写了一个函数来节省我不得不一直查找.NET参考页的麻烦。 如果你看看页面,你会看到有一个解压缩的方法。
.NET 4.5是必需的,我强烈鼓励你升级PowerShell。 每个版本都带来了大量的改进。 它也certificate了使用.NET是有效的,它已经与stream行的第三方压缩应用程序进行了强烈的基准testing。
这个函数是非常自我解释的,只要确保在调用它之前先运行函数。 如果你需要的话,这是一个简短的语法概要:
New-ZipFile {DirToZip} {ZipfilePathAndName} {fastORoptimalORnocompression} {IncludeBaseDirectoryInZip($true or $false)}
因此,要使用最佳压缩方式将E:\stuff压缩到D:\zipfile.zip并在zipfile中包含basedir .\stuff ,您可以运行:
New-ZipFile 'E:\stuff' 'D:\zipfile.zip' Optimal $true
这是function:
#region function New-ZipFile <# .Synopsis Creates a zip file from a directory. .DESCRIPTION Creates a zip file from a directory. Options include compression level and wether or not to include the base directory. Class reference: https://msdn.microsoft.com/en-us/library/hh875104(v=vs.110).aspx .EXAMPLE New-ZipFile c:\dir c:\zipfile.zip Fastest $True .EXAMPLE New-ZipFile c:\dir c:\zipfile.zip Optimal $True .INPUTS Inputs to this cmdlet (if any) .OUTPUTS Output from this cmdlet (if any) .NOTES General notes .COMPONENT The component this cmdlet belongs to .ROLE The role this cmdlet belongs to .FUNCTIONALITY The functionality that best describes this cmdlet #> function New-ZipFile { Param ( # The directory to zip. [Parameter(Mandatory=$true, Position=0)] [ValidateNotNullOrEmpty()] [string]$DirPath, # The zipfile name. [Parameter(Mandatory=$true, Position=1)] [ValidateNotNullOrEmpty()] [string]$ZipFilePath, # Specifies values that indicate whether a compression operation emphasizes speed or compression size. [Parameter(Mandatory=$true, Position=2)] [ValidateNotNullOrEmpty()] [ValidateSet("Fastest", "NoCompression", "Optimal", ignorecase=$True)] [string]$CompressionLevel, # $True to include the directory name from sourceDirectoryName at the root of the archive; $False to include only the contents of the directory. [Parameter(Mandatory=$true, Position=3)] [ValidateNotNullOrEmpty()] [bool]$includeBaseDir ) #Load the .NET 4.5 zip-assembly [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null # Zip the directory. [System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir) } #endregion
编辑:脚本的人写了一篇文章,他显示了加载程序集的替代方式 ,它看起来有点干净。