我已經用一些檔案加密了 .zip 存檔。以后的存檔內容必須由不知道加密密碼的人檢查。有沒有辦法在powershell中做到這一點?
Ubuntu有zip -sf myfile.zip命令,但我在 powershell 中找不到任何類似的東西。
uj5u.com熱心網友回復:
如果您只是想列出 zip 內容,那么這個函式就可以了。至于提取 Zip 內容,目前不支持加密的 Zip 。有第三方 PowerShell 模塊以及庫可以做到這一點。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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523415.html
標籤:视窗电源外壳加密system.io.compression
