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