我已经用一些文件加密了.Zip存档。之后,存档内容必须由不知道加密密码的人检查。在PowerShell中有什么方法可以做到这一点吗?
zip -sf myfile.zip
ldioqlga1#
如果你只是想列出压缩文件的内容,那么这个函数就可以了。至于提取Zip内容,从今天起,ZipArchivedoes not support encrypted Zips。不过,也有第三方PowerShell模块和libraries可以做到这一点。
ZipArchive
function Get-ZipContent { [CmdletBinding()] param( [Parameter(ParameterSetName = 'Path', Position = 0, Mandatory, ValueFromPipeline)] [string[]] $Path, [Parameter(ParameterSetName = 'LiteralPath', Mandatory, ValueFromPipelineByPropertyName)] [Alias('PSPath')] [string[]] $LiteralPath ) begin { Add-Type -AssemblyName System.IO.Compression } process { try { $items = switch($PSCmdlet.ParameterSetName) { Path { Get-Item $Path -ErrorAction Stop } LiteralPath { $LiteralPath | Get-Item -ErrorAction Stop } } foreach($item in $items) { try { $fs = $item.OpenRead() $zip = [System.IO.Compression.ZipArchive]::new($fs) $zip.Entries | Select-Object @{ N='Source'; E={ $item.FullName }}, * } 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
1条答案
按热度按时间ldioqlga1#
如果你只是想列出压缩文件的内容,那么这个函数就可以了。至于提取Zip内容,从今天起,
ZipArchive
does not support encrypted Zips。不过,也有第三方PowerShell模块和libraries可以做到这一点。用途:
为了进一步扩大用法,因为它似乎不是很清楚: