在powershell中使用7z解压文件

问题描述 投票:0回答:6

在 powershell 中使用 7z 解压文件的命令是什么?

set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
sz x  $zipfilePath $destinationUnzipPath -aoa -r;

该命令工作正常,但它说没有要处理的文件,一切正常而不是解压缩文件?

powershell 7zip
6个回答
11
投票

这终于对我有用了

sz x -o$destinationUnzipPath $zipfilePath -r ;


9
投票

我不想使用别名、函数或

Start-Process
。在网上浏览了一下后,我发现了这个宝石(我不记得在哪里):

& ${env:ProgramFiles}\7-Zip\7z.exe x $zipfilePath "-o$($destinationUnzipPath)" -y

如果您不想看到7z的消息,可以在末尾添加

> $null


8
投票

有了 7zip PowerShell 模块,现在就没有麻烦了

#   Install 7zip module

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Set-PSRepository -Name 'PSGallery' -SourceLocation "https://www.powershellgallery.com/api/v2" -InstallationPolicy Trusted
Install-Module -Name 7Zip4PowerShell -Force

#   Extract 7zip file

$sourcefile = "c:\source\sample.7z"
Expand-7Zip -ArchiveFileName $sourcefile -TargetPath 'c:\destinaation'

1
投票

我的用例略有不同,因为我的目录中有多个需要提取的 tar 文件。我分享它是因为也可以使用相同的命令或稍微修改一下即可使用:

这是通过 Powershell 在 Windows 10 上为我工作的命令:

注意:您当然需要根据您的用例更改以下路径。

$srcFolderPathWithTar="C:\SomeFilder\has\multiple\tar\files"
$targ="C:\Users\your_user_name\Downloads\new" 

. "C:\Program Files\7-Zip\7z.exe"  x "-o$($targ)" "$srcFolderPathWithTar" -r -y;

0
投票

我使用了

"fullname"
,其中包括路径。
另外,我必须将 PowerShell 中的目录更改为提取数据的输出目录,即
D:\temp

我不相信在这个时代将一堆文件从不同的文件夹复制或提取到一个位置是一项复杂的任务。

$rars = (Get-ChildItem "D:\Path\To\folder" -Recurse *.rar).fullname
foreach ($rar in $rars) {& "C:\Program Files\7-Zip\7z.exe" e $rar}

0
投票

这个命令效果很好。它将 zip 文件解压到目标目录:

$DESTINATION_DIR="C:\my_destination"
& "${env:ProgramFiles}\7-Zip\7z.exe" x $ZIP_FILE "-o$($DESTINATION_DIR)" -aoa -r
© www.soinside.com 2019 - 2024. All rights reserved.