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)
小智 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)
Kev*_*hal 21
DotNetZip将允许您从 PowerShell 执行此操作。它不是单行的,但该库将允许您编写所需的 PowerShell 脚本。
您还可以使用 COM 接口,请参阅使用 Windows PowerShell 压缩文件然后打包 Windows Vista 边栏小工具。
谷歌搜索“zip powershell”或“unzip powershell”也可能会得到有用的结果。
小智 19
我知道这是一个非常古老的问题,但我刚刚在 Twitter 上看到它的链接,我想我会发布当前的答案。
PowerShell 5 目前可在 Windows 10 上或通过Windows Management Framework 5 Production Preview 使用,带有两个用于“压缩”和“解压缩”的内置 cmdlet:
我找到了最简单的解决方案,即使用我已经使用多年并在 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 包装器会很直接,但实际上我从不需要它,所以我没有打扰。
我也喜欢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)
| 归档时间: |
|
| 查看次数: |
136538 次 |
| 最近记录: |