在Powershell 4中压缩和解压缩文件

时间:2016-11-19 10:54:14

标签: powershell zip unzip powershell-v4.0

我使用的是Windows Server 2012 R2(64位)。我有powershell版本4可用。我正在尝试压缩和解压缩文件。当我尝试Write-Zip命令时,它会抛出以下错误:

Write-Zip:术语' Write-Zip'不被识别为cmdlet,函数,脚本文件或可操作的名称 程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

我应该怎么做才能修复它?我是否需要在服务器中安装zip / winrar?或者是否有其他命令执行zip / unzip文件?

5 个答案:

答案 0 :(得分:18)

Write-Zip似乎是http://pscx.codeplex.com/的一部分,需要单独安装才能使用它。

但是,如果您只想从文件夹创建Zip存档,则可以运行

$source = "c:\temp\source"
$archive = "c:\temp\archive.zip"

Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($source, $archive)

这利用了.NET Framework类CreateFromDirectory中的ZipFile方法。它从位于$source文件夹内的文件创建zip存档,并创建$archive变量中定义的存档。注意,ZipFile类是在.NET Framework 4.5中引入的

答案 1 :(得分:3)

您可以使用自定义PowerShell对象New-Object -ComObject Shell.Application并使用标记复制文件以解压缩。

$filePath = "foo.zip"
$shell = New-Object -ComObject Shell.Application
$zipFile = $shell.NameSpace($filePath)
$destinationFolder = $shell.NameSpace("C:\Program Files\WindowsPowerShell\Modules")

$copyFlags = 0x00
$copyFlags += 0x04 # Hide progress dialogs
$copyFlags += 0x10 # Overwrite existing files

$destinationFolder.CopyHere($zipFile.Items(), $copyFlags)

信用来源https://github.com/hashicorp/best-practices/blob/master/packer/scripts/windows/install_windows_updates.ps1#L12-L22

这不适用于windows'核心'版。如果可能,请升级到powershell 5并使用Expand Archive,因为它更简单。

答案 2 :(得分:0)

如果您可以升级到PowerShell V5(https://www.microsoft.com/en-us/download/details.aspx?id=50395),它本身就有它们。 https://richardspowershellblog.wordpress.com/2014/10/25/powershell-5-zip-and-unzip/

对于PowerShell版本4,您可以使用此搜索http://www.powershellgallery.com/items?q=zip&x=0&y=0。这也可以满足您的需求:https://www.powershellgallery.com/packages/Microsoft.PowerShell.Archive/1.0.1.0

要安装模块,您需要输入:

install-module -name <module name>
  • powershellgallery.com是一个免费上传的网站。请在运行之前检查并理解模块。

希望这会有所帮助。 谢谢,蒂姆。

答案 3 :(得分:0)

Write-Zip安装可能未正确执行。错误地手动编辑环境参数PSModulePath可能导致它:

原始值错误:

PSModulePath = %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;C:\Program Files\Intel\

物有所值(可以解决问题)

PSModulePath = C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\

答案 4 :(得分:-1)

应该在PS4下工作。请参阅New-Zip[CmdletBinding()] Param( [Parameter(Mandatory=$True)] [ValidateScript({Test-Path -Path $_ })] [string]$sourceDirectory, [Parameter(Mandatory=$True)] [ValidateScript({-not(Test-Path -Path $_ -PathType Leaf)})] [string]$destinationFile, [Parameter(Mandatory=$True)] [int]$noOlderThanHours ) function New-Zip { param([string]$zipfilename) set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).IsReadOnly = $false } function Add-Zip { param([string]$zipfilename) if(-not (test-path($zipfilename))) { set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).IsReadOnly = $false } $shellApplication = new-object -com shell.application $zipPackage = $shellApplication.NameSpace($zipfilename) foreach($file in $input) { $zipPackage.CopyHere($file.FullName) Start-sleep -milliseconds 500 } } $oldest = (Get-Date) - (New-TimeSpan -Hours $noOlderThanHours) $filesToZip = dir $sourceDirectory -Recurse | Where-Object {$_.lastwritetime -gt $oldest} Write-Host Going to zip following files $filesToZip | foreach {Write-Host $_.FullName} $filesToZip| Add-Zip $destinationFile 函数

<Window.Resources>
    <SolidColorBrush x:Key="TabItem.Selected.Background" Color="#424E5A"/>
    <SolidColorBrush x:Key="TabItem.Selected.Border" Color="#424E5A"/>
    <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
        <Setter Property="Padding" Value="2"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
        <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabControl}">
                    <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="ColumnDefinition0"/>
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                            <RowDefinition x:Name="RowDefinition1" Height="*"/>
                        </Grid.RowDefinitions>
                        <TabPanel x:Name="headerPanel" Background="#424E5A" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                        <Border x:Name="contentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                            <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="TabStripPlacement" Value="Bottom">
                            <Setter Property="Grid.Row" TargetName="headerPanel" Value="1"/>
                            <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                            <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                            <Setter Property="Margin" TargetName="headerPanel" Value="2,0,2,2"/>
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Left">
                            <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                            <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                            <Setter Property="Grid.Column" TargetName="headerPanel" Value="0"/>
                            <Setter Property="Grid.Column" TargetName="contentPanel" Value="1"/>
                            <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                            <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            <Setter Property="Margin" TargetName="headerPanel" Value="2,2,0,2"/>
                        </Trigger>
                        <Trigger Property="TabStripPlacement" Value="Right">
                            <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                            <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                            <Setter Property="Grid.Column" TargetName="headerPanel" Value="1"/>
                            <Setter Property="Grid.Column" TargetName="contentPanel" Value="0"/>
                            <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                            <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                            <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                            <Setter Property="Margin" TargetName="headerPanel" Value="0,2,2,2"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="templateRoot" Property="Background" Value="#424E5A" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>