在PowerShell 4中压缩和解压文件

18

我正在使用Windows Server 2012 R2(64位)操作系统,其中安装了PowerShell版本4。我想要压缩和解压文件。当我尝试运行Write-Zip命令时,会抛出以下错误:

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

我该怎么做才能解决这个问题?我需要在服务器上安装zip/winrar吗?还是有其他命令可以用来压缩/解压文件?


我需要压缩一个文件夹。这个文件夹有很多子文件夹和文件。有人能告诉我如何在PowerShell和Windows命令行(cmd)中压缩它吗? - Raji
5个回答

34

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类ZipFile中的CreateFromDirectory方法。它将位于$source文件夹内的文件创建为一个zip归档文件,并按照$archive变量的定义创建归档文件。请注意,ZipFile类是在.NET Framework 4.5中引入的。


3
这个回答非常准确,并且不需要升级或安装任何额外的软件即可很好地工作。非常棒的回答。 - takesides

9

您可以使用自定义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,因为它更简单。


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\

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 是一个免费上传的网站。在运行模块之前,请先进行检查和了解。

希望这有所帮助。 谢谢,Tim。


嗨Tim,非常感谢您的回复。但是当我执行“Install-Module”命令时,它会抛出与“Write-Zip”命令相同的错误,即未识别该命令。 - Raji
1
嗨Raji,抱歉。刚刚检查了一下,您需要PowerShell V5才能从poweshellgallery.com安装。或者,使用此URL中列出的MSI安装程序。https://msdn.microsoft.com/en-us/powershell/gallery/readme?f=255&MSPPError=-2147217396 - Tim Haintz

0

应该在PS4下工作。请查看Add-ZipNew-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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接