使用 7-Zip 和 PowerShell 就地提取存档文件的内容

Extracting contents of archived files in place using 7-Zip and PowerShell

我想将所有 *.Z 文件提取到它们当前目录中的位置(7-Zip 支持 LZW/UNIX 压缩)。

我不明白为什么这不起作用。我知道如何使用 PowerShell 递归地获取文件名和文件所在目录的完整路径。我也知道如何使用 7-zip 就地提取文件。但是试图将这两者放在一起是行不通的。这是我拥有的:

Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o$_.Directory}

我也试过:

Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o$($_.Directory)}

Get-ChildItem -Recurse *.Z | foreach ($_) {7z.exe e $_.FullName -o${_.Directory}}

这是通过FullName获取我想要的文件,Directory是正确的路径(no space between the o and directory is how 7-zip expects the output directory)。但是,它一直尝试将文件输出到 .Z 路径而不是目录。我知道这样做是对的

Get-ChildItem -Recurse *.Z | Select Directory

这就是我想要它去的地方。

我假设问题是没有 space 但不确定为什么它会得到相同的目录。我什至手动定义了一个变量并毫无问题地传递了它。

所以问题是 7-zip 说没有文件要处理,当我想输出到同一目录但不确定为什么时,似乎 Directory 变量没有被传递,但文件名再次传递,这没有意义.每个文件的示例输出:

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21

Scanning the drive for archives:
1 file, 226 bytes (1 KiB)

Extracting archive: E:\Files14\more_cowbell\myfancyunixcompresseddocument.txt.Z
--
Path = E:\Files14\more_cowbell\myfancyunixcompresseddocument.txt.Z
Type = Z


No files to process
Everything is Ok

Files: 0
Size:       0
Compressed: 226

如果我指定一个特定的输出目录(无变量),它工作正常,但由于许多子目录和它们之间的文件名冲突,我希望将它们提取到位。

我想将所有 *.Z 文件提取到它们当前目录中的位置(7-Zip 支持 LZW/UNIX 压缩)。

-o 参数应在双引号字符串内,变量 $($var)

Get-ChildItem -Recurse *.z | %{ 7z e $_.FullName "-o$($_.Directory)"}