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

God*_*ter 4 powershell zip

我目前正在尝试编写一个powershell函数,该函数与Lync powershell cmdlet"Export-CsConfiguration -AsBytes"的输出一起使用.使用Lync Cmdlet的隐式Powershell远程处理时,-AsBytes标志是使用Export-CsConfiguration cmdlet的唯一方法,它返回一个Byte数组,如果您使用"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
Run Code Online (Sandbox Code Playgroud)

而不是做:

$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
Run Code Online (Sandbox Code Playgroud)

或者我是SOL?

God*_*ter 8

在这里回答我自己的问题,以防它对其他人有用:( NB需要.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
}
Run Code Online (Sandbox Code Playgroud)

这正是我需要的.

谢谢大家:)