繁体   English   中英

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

[英]Zip and Unzip File in Powershell 4

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

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

我应该怎么做才能修复它? 我需要在服务器中安装 zip/winrar 吗? 或者有没有其他命令可以压缩/解压缩文件?

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

您可以使用自定义 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 'core' 版本。 如果可能,升级到 powershell 5 并使用Expand Archive因为它要简单得多。

如果您可以升级到 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 是一个免费上传网站。 请在运行前检查并理解模块。

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

应该在 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

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\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM