如何在 Powershell 中压缩/解压缩文件?

Blu*_*ene 104 powershell compression command-line-interface

是否有单行程序可以在 PowerShell 中压缩/解压缩文件 (*.zip)?

Ame*_*een 154

这就是您可以在没有任何外部工具的情况下完全从 Powershell 执行此操作的方法。这会将名为 test.zip 的文件解压缩到当前工作目录中:

$shell_app=new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace((Get-Location).Path + "\$filename")
$destination = $shell_app.namespace((Get-Location).Path)
$destination.Copyhere($zip_file.items())
Run Code Online (Sandbox Code Playgroud)

  • 使用 $destination.Copyhere($zip_file.items(), 0x10) 覆盖现有文件。0x4 隐藏对话框,0x14 将这些组合起来覆盖并隐藏对话框。 (26认同)
  • 这应该是公认的答案。 (6认同)
  • 行 `$destination.Copyhere($zip_file.items())` 执行实际解压缩。 (3认同)
  • 如果需要,您可以将上述内容打包成一个函数:`function unzip($filename) { if (!(test-path $filename)) { throw "$filename does not exist" } $shell = new-object -com shell.application $shell.namespace($pwd.path).copyhere($shell.namespace((join-path $pwd $filename)).items()) }` (2认同)
  • 当 zip 文件只包含一个文件夹(项目为空)时,这对我来说失败了 (2认同)

小智 56

现在在 .NET Framework 4.5 中,有一个ZipFile类,您可以像这样使用:

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)
Run Code Online (Sandbox Code Playgroud)

  • 使用 Add-Type 加载程序集,例如:Add-Type -AssemblyName System.IO.Compression.FileSystem (3认同)
  • 请注意,相对文件位置将使用 *.NET* 当前目录,而不是 PowerShell 目录。请参阅[此处](http://stackoverflow.com/q/11246068/1394393)。最好只使用 `(Resolve-Path $someDir).Path` 参数。 (3认同)
  • @JamesDunne - 如果您没有其他需要保留的文件,可以使用“Remove-Item -Recurse $TargetFolder”。否则,你想要的都可以做到,但这将是不平凡的。您需要打开 zip 进行阅读,然后遍历 zip,删除任何以前的目标对象并解压缩新对象。对我来说幸运的是,简单的解决方案有效。;) (2认同)

Kev*_*hal 21

DotNetZip将允许您从 PowerShell 执行此操作。它不是单行的,但该库将允许您编写所需的 PowerShell 脚本。

您还可以使用 COM 接口,请参阅使用 Windows PowerShell 压缩文件然后打包 Windows Vista 边栏小工具

谷歌搜索“zip powershell”或“unzip powershell”也可能会得到有用的结果。

  • -1 用于建议 Google 搜索。这是在 Google 搜索“unzip powershell”中的顶级 StackExchange 结果 (6认同)

小智 19

我知道这是一个非常古老的问题,但我刚刚在 Twitter 上看到它的链接,我想我会发布当前的答案。

PowerShell 5 目前可在 Windows 10 上或通过Windows Management Framework 5 Production Preview 使用,带有两个用于“压缩”和“解压缩”的内置 cmdlet:

  • 为旧问题提供新信息是一件*好事*。;) +1 (4认同)

Kev*_*lby 18

您可能希望查看PowerShell 社区扩展 (PSCX),其中包含专门用于此的 cmdlet。


Bri*_*ter 5

我找到了最简单的解决方案,即使用我已经使用多年并在 UNIX 环境中使用的 infozip 二进制文件。

PS> zip -9r ../test.zip * 
PS> cd .. 
PS> unzip -t test.zip Archive:  test.zip
    testing: LinqRepository/          OK
    testing: LinqRepository/ApplicationService.cs   OK
    testing: LinqRepository/bin/      OK 
... 
No errors detected in compressed data of test.zip.
Run Code Online (Sandbox Code Playgroud)

在文本输出周围放置一个 powershell 包装器会很直接,但实际上我从不需要它,所以我没有打扰。

http://www.info-zip.org/


Nat*_*ley 5

我也喜欢Info-ZIP(大多数其他 Zip 实用程序中的 Zip 引擎)和7-Zip,这是另一个具有 GUI 和命令行 Zip 实用程序的最爱。关键是,有一些很好的命令行实用程序适用于大多数 PowerShell 任务。

有一些运行命令行实用程序的技巧,这些实用程序不是使用 PowerShell 构建的:

  • 运行名称中以数字开头的可执行文件,并在它前面加上与号 (&)。

    &7zip.exe

  • 用引号将每个令牌包装起来,实用程序希望从命令行看到。

    &"c:\path with space\SomeCommand.exe" "/parameter2" "/parameter2" "parameter2's Value" "Value2 `" 带引号"

尝试这个:

zip filename.zip (Get-ChildItem somepath\*)
Run Code Online (Sandbox Code Playgroud)

甚至:

zip filename.zip (Get-Content ListOfFiles.txt)
Run Code Online (Sandbox Code Playgroud)