gpt4 book ai didi

powershell - 有没有办法使用 Powershell 操作内存中的 zip 文件内容?

转载 作者:行者123 更新时间:2023-12-04 03:09:54 32 4
gpt4 key购买 nike

我目前正在尝试编写一个 powershell 函数,该函数与 Lync powershell cmdlet“Export-CsConfiguration -AsBytes”的输出一起使用。在 Lync Cmdlet 中使用隐式 Powershell 远程处理时,-AsBytes 标志是使用 Export-CsConfiguration cmdlet 的唯一方法,它返回一个字节数组,如果您使用“Set-Content -Encoding Byte”将其写入磁盘, 生成一个 zip 文件。

我想知道是否有办法将字节数组的内容扩展到包含在该 zip 中的两个文件中,但只能在内存中进行。我对长期保存 zip 文件并不感兴趣,因为它经常更改,并且将文件内容写入磁盘只是为了再次直接读取它们以便我可以对未压缩的内容执行某些操作似乎非常错误对我来说。

那么是否有某种方式可以避免写入磁盘:

$ZipFileBytes = Export-CsConfiguration -AsBytes
# Made up Powershell function follows:
[xml]DocItemSet = Extract-FileFromInMemoryZip -ByteArray $ZipFileBytes -FileInsideZipIWant "DocItemSet.xml"
# Do stuff with XML here

而不是做:
$ZipFileBytes = Export-CsConfiguration -AsBytes | Set-Content -Encoding Byte "CsConfig.zip"
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory("CsConfig.zip", "C:\Temp")
[xml]$DocItemSet = New-Object Xml.XmlDocument
$DocItemSet.Load("C:\Temp\DocItemSet.xml")
# Do stuff with XML here

还是我是SOL?

最佳答案

在这里回答我自己的问题,以防它对其他人有用:(注:需要 .NET 4.5)

看起来将 System.IO.Compression.ZipArchive 与 System.IO.Memorystream 结合使用是前进的方向。我现在有这个:

Function Load-CsConfig{
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression') | Out-Null

$ZipBytes = Export-CsConfiguration -AsBytes
$ZipStream = New-Object System.IO.Memorystream
$ZipStream.Write($ZipBytes,0,$ZipBytes.Length)
$ZipArchive = New-Object System.IO.Compression.ZipArchive($ZipStream)
$ZipEntry = $ZipArchive.GetEntry('DocItemSet.xml')
$EntryReader = New-Object System.IO.StreamReader($ZipEntry.Open())
$DocItemSet = $EntryReader.ReadToEnd()
return $DocItemSet
}

这正是我需要的。

谢谢大家:)

关于powershell - 有没有办法使用 Powershell 操作内存中的 zip 文件内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143435/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com