在 Powershell 4 中压缩和解压缩文件 [英] Zip and Unzip File in Powershell 4
问题描述
我使用的是 Windows Server 2012 R2(64 位).我有可用的 powershell 版本 4.我正在尝试压缩和解压缩文件.当我尝试 Write-Zip 命令时,它会引发以下错误:
I am using Windows Server 2012 R2 (64 bit). I have powershell version 4 available in it. I am trying to zip and unzip files. When I try Write-Zip command, it throws me following error:
Write-Zip : 术语Write-Zip"不被识别为 cmdlet、函数、脚本文件或可操作的名称程序.检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试.
Write-Zip : The term 'Write-Zip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
我该怎么做才能修复它?我需要在服务器中安装 zip/winrar 吗?或者有没有其他命令可以压缩/解压缩文件?
What should I do to fix it? Do I need to install zip/winrar in the server? Or is there any other command do zip/unzip files?
推荐答案
Write-Zip
似乎是 http://pscx.codeplex.com/ 需要单独安装才能使用.
Write-Zip
seems to be part of http://pscx.codeplex.com/ that require a separate installation before you can use it.
但是,如果您只想从文件夹创建 Zip 存档,则可以运行
However, if you just want to create a Zip archive from a folder, you could just run
$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 中引入的
This utilizes the CreateFromDirectory
method from the .NET Framework class ZipFile
. It creates a zip archive from the files located inside the $source
folder and creates an archive as defined in the $archive
variable. Note, ZipFile class was introduced in .NET Framework 4.5
这篇关于在 Powershell 4 中压缩和解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!