5

我只是尝试在 powershell 中打开一个 zip 存档并将其中的文件移动到特定位置。但它总是只移动 zip 文件夹。我究竟做错了什么 ?

这就是我现在拥有的:

Get-ChildItem C:\zipplayground\*.zip | % {"C:\Program Files (x86)\7-Zip\7zG.exe";
Move-Item $_ C:\unzipplayground\}
4

4 回答 4

11

我相信正确的答案应该是这样的:

Get-ChildItem C:\zipplayground\*.zip | % {& "C:\Program Files\7-Zip\7z.exe" "x" $_.fullname "-oC:\unzipplayground"}

Alroc 几乎是正确的,但$_.fullname引号之间不起作用,而且他缺少-o7z 的参数。我正在使用7z.exe而不是7zg.exe,这样可以正常工作。

作为参考,可以在这里找到命令行帮助:http: //sevenzip.sourceforge.jp/chm/cmdline/ 基本上,x代表“eXtract”和-o“输出目录”

于 2013-08-02T11:59:28.560 回答
4

获取7z.exe路径的函数

function Get-7ZipExecutable
{
    $7zipExecutable = "C:\Program Files\7-Zip\7z.exe"
    return $7zipExecutable
}

压缩目标设置为的文件夹的功能

function 7Zip-ZipDirectories
{
    param
    (
        [CmdletBinding()]
        [Parameter(Mandatory=$true)]
        [System.IO.DirectoryInfo[]]$include,
        [Parameter(Mandatory=$true)]
        [System.IO.FileInfo]$destination
             )

    $7zipExecutable = Get-7ZipExecutable

     # All folders in the destination path will be zipped in .7z format
     foreach ($directory in $include)
    {
        $arguments = "a","$($destination.FullName)","$($directory.FullName)"
    (& $7zipExecutable $arguments)

        $7ZipExitCode = $LASTEXITCODE

        if ($7ZipExitCode -ne 0)
        {
            $destination.Delete()
            throw "An error occurred while zipping [$directory]. 7Zip Exit Code was [$7ZipExitCode]."
        }
    }

    return $destination
}

解压文件的功能

function 7Zip-Unzip
{
    param
    (
        [CmdletBinding()]
        [Parameter(Mandatory=$true)]
        [System.IO.FileInfo]$archive,
        [Parameter(Mandatory=$true)]
        [System.IO.DirectoryInfo]$destinationDirectory
    )

    $7zipExecutable = Get-7ZipExecutable
    $archivePath = $archive.FullName
    $destinationDirectoryPath = $destinationDirectory.FullName

    (& $7zipExecutable x "$archivePath" -o"$destinationDirectoryPath" -aoa -r)

    $7zipExitCode = $LASTEXITCODE
    if ($7zipExitCode -ne 0)
    {
        throw "An error occurred while unzipping [$archivePath] to [$destinationDirectoryPath]. 7Zip Exit Code was [$7zipExitCode]."
    }

    return $destinationDirectory
}
于 2016-04-25T10:29:22.507 回答
0

我没有要测试的 7Zip,但我认为它失败了,因为您没有告诉 7Zip 要操作什么,而是您自己将 ZIP 文件移动到目的地。尝试这个:

Get-ChildItem C:\zipplayground\*.zip | % {invoke-expression "C:\Program Files (x86)\7-Zip\7zG.exe x $_.FullName c:\unzipplayground\";}
于 2013-08-02T11:20:24.900 回答
0

如果您想避免像 7zip 这样的开源 exe/dll,请为 powershell 安装 PSCX 模块并使用 expand-archive。请注意,PSCX 要求至少为 .net 4(我使用 4.5)和 powershell 3.0

http://pscx.codeplex.com/

于 2013-08-02T11:48:18.483 回答