PowerShell 使用 7z.dll 和 SevenZipSharp.dll 解压缩文件

Win11 系统,通过 dependency walker (depends.exe)查看 7z.dll 发现有如下的导出函数

CreateObject
GetHandlerProperty2
GetHandlerProperty
GetMethodProperty
GetNumberOfFormats
GetNumberOfMethods
SetLargePageMode

或者
"C:\Users\geyee\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\x86_amd64\dumpbin.exe" /exports "C:\Program Files (x86)\筑龙软件\某XXX平台标书查看工具\7z.dll"
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

Dump of file C:\Program Files (x86)\筑龙软件\某XXX平台标书查看工具\7z.dll

File Type: DLL

Section contains the following exports for 7z.dll

00000000 characteristics
4DAC88C7 time date stamp Tue Apr 19 02:53:59 2011
    0.00 version
       1 ordinal base
       7 number of functions
       7 number of names

ordinal hint RVA      name

      1    0 00008905 CreateObject
      3    1 00008888 GetHandlerProperty
      2    2 00008793 GetHandlerProperty2
      4    3 0006CB80 GetMethodProperty
      5    4 0000889E GetNumberOfFormats
      6    5 0006CCC0 GetNumberOfMethods
      7    6 0000896B SetLargePageMode

Summary

    C000 .data
   17000 .rdata
    9000 .reloc
   18000 .rsrc
    1000 .sxdata
   A9000 .text

image

不实现 Com 接口,而利用 dll 的 PowerShell 脚本有

# 加载必要的程序集
Add-Type -Path "C:\Program Files (x86)\筑龙软件\某XX电子交易平台标书查看工具\SevenZipSharp.dll"

# 设置7z.dll路径
[SevenZip.SevenZipBase]::SetLibraryPath("C:\Program Files (x86)\筑龙软件\某XX电子交易平台标书查看工具\7z.dll")

# 解压文件
function Expand-7Zip {
    param(
        [Parameter(Mandatory = $true)][string]$ArchivePath,
        [Parameter(Mandatory = $true)][string]$OutputDirectory,
        [SecureString]$Password = $null
    )
    
    try {
        if (-not (Test-Path -LiteralPath $OutputDirectory)) {
            New-Item -ItemType Directory -Force -Path $OutputDirectory | out-null
        }
        if ($Password) {
            $plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
                [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
            )
            $extractor = New-Object SevenZip.SevenZipExtractor($ArchivePath, $plainPassword)
        }
        else {
            $extractor = New-Object SevenZip.SevenZipExtractor($ArchivePath)
        }
      
                    
        $extractor.ExtractArchive($OutputDirectory)
       
        
        Write-Host "解压成功: $ArchivePath -> $OutputDirectory" -ForegroundColor Green
        return $true
    }
    catch {
        Write-Error "解压失败: $($_.Exception.Message)"
        return $false
    }
    finally {
        if ($null -ne $extractor) { $extractor.Dispose() }
    }
    
}

示例:解压无密码的压缩包

$archive = "C:\path\to\your\archive.7z"
$output = "C:\path\to\output\folder"
Expand-7Zip -ArchivePath $archive -OutputDirectory $output

示例:解压有密码的压缩包

$archive = "C:\path\to\your\protected_archive.7z"
$output = "C:\path\to\output\folder"
$password = Read-Host "请输入压缩包密码" -AsSecureString
Expand-7Zip -ArchivePath $archive -OutputDirectory $output -Password $password

比如 expand-7Zip "D:\mysoftware\temp_test_extract\test.7z" D:\extracted -Password $(read-host "pass" -AsSecureString)
如果降低安全需求的话,可以将 [SecureString]$Password 替换成 [String]$Password,并修改
if ($Password) {
$plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
)
$extractor = New-Object SevenZip.SevenZipExtractor($ArchivePath, $plainPassword)
}

if ($Password) {
$extractor = New-Object SevenZip.SevenZipExtractor($ArchivePath, $Password)
}

注意脚本需要在x86环境下运行,32位的PowerShell。
开始菜单运行栏输入 "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NoExit -NoLogo

posted @ 2025-08-28 15:55  geyee  阅读(19)  评论(0)    收藏  举报