windows - 我可以在不使用密码的情况下检查加密的 zip 存档的内容吗?

标签 windows powershell encryption zip system.io.compression

我已经用一些文件加密了 .zip 存档。以后的存档内容必须由不知道加密密码的人来检查。有没有办法在 powershell 中做到这一点?

Ubuntuzip -sf myfile.zip命令,但我在powershell中找不到任何类似的命令。

最佳答案

如果您只是想列出 zip 内容,那么这个函数就可以了。至于提取 Zip 内容,ZipArchive does not support encrypted Zips截至今日。还有第三方 PowerShell 模块以及 libraries但这可以做到这一点。

function Get-ZipContent {
    [CmdletBinding()]
    param(
        [Parameter(ParameterSetName = 'Path', Position = 0, Mandatory, ValueFromPipeline)]
        [string[]] $Path,

        [Parameter(ParameterSetName = 'LiteralPath', Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('PSPath')]
        [string[]] $LiteralPath,

        [Parameter()]
        [switch] $Force
    )

    begin {
        Add-Type -AssemblyName System.IO.Compression
    }
    process {
        try {
            $arguments = switch($PSCmdlet.ParameterSetName) {
                Path { $Path, $Force.IsPresent, $false }
                LiteralPath { $LiteralPath, $Force.IsPresent, $true }
            }

            foreach($item in $ExecutionContext.InvokeProvider.Item.Get.Invoke($arguments)) {
                try {
                    $fs  = $item.OpenRead()
                    $zip = [IO.Compression.ZipArchive]::new($fs, [IO.Compression.ZipArchiveMode]::Read)
                    foreach($entry in $zip.Entries) {
                        $entry.PSObject.Properties.Add([psnoteproperty]::new('Source', $item.FullName))
                        $entry
                    }
                }
                catch {
                    $PSCmdlet.WriteError($_)
                }
                finally {
                    $zip, $fs | ForEach-Object Dispose
                }
            }
        }
        catch {
            $PSCmdlet.WriteError($_)
        }
    }
}

用法:

PS ..\pwsh> Get-ZipContent path\to\myfolder\*.zip
PS ..\pwsh> Get-ChildItem path\to\things -Recurse -Filter *.zip | Get-ZipContent 

为了进一步扩大用途,因为它似乎不太清楚:

# load the function in memory:
PS ..\pwsh> . ./theFunctionisHere.ps1

# call the function giving it a path to a zip:
PS ..\pwsh> Get-ZipContent ./thing.zip

Source             : path/to/pwsh/thing.zip
Archive            : System.IO.Compression.ZipArchive       
Crc32              : 0
IsEncrypted        : True
CompressedLength   : 165
ExternalAttributes : 32
Comment            : 
FullName           : other thing.txt
LastWriteTime      : 10/29/2022 10:31:30 AM -03:00
Length             : 446
Name               : other thing.txt

Source             : path/to/pwsh/thing.zip
Archive            : System.IO.Compression.ZipArchive
Crc32              : 0
IsEncrypted        : True
CompressedLength   : 165
ExternalAttributes : 32
Comment            : 
FullName           : thing.txt
LastWriteTime      : 10/29/2022 10:31:30 AM -03:00
Length             : 446
Name               : thing.txt

注意:上述函数的改进版本可作为 PSCompression Module 的一部分提供。 .

关于windows - 我可以在不使用密码的情况下检查加密的 zip 存档的内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74248239/

相关文章:

c - 如何使用Windows和C从URL获取数据?

powershell - Powershell "Cannot set "属性错误“因为只有字符串可以用作设置 XmlNode 属性的值

powershell - 连续运行中PowerShell中的内存泄漏

Java套接字通信——传输加密数据

xml - 为 SAML 签名和加密创建 x509.v3 自签名证书,Windows 8 R2

java - gcloud Preview 应用程序在 Windows 上运行 "bad port Access Denied"错误

c++ - 对逆向工程隐藏字符数组

使用 AES 的 Java 卡加密返回 6F00

c# - 为什么我的 64 位服务以 32 位运行?

powershell - 如何管理处理从另一个对象扩展的字符串列表?