Is there a way of manipulating zip file contents in memory with Powershell?

后端 未结 3 1773
时光说笑
时光说笑 2021-01-03 06:46

I\'m currently trying to write a powershell function which works with the output of the Lync powershell cmdlet \"Export-CsConfiguration -AsBytes\". When using implicit Power

3条回答
  •  温柔的废话
    2021-01-03 07:05

    Answering my own question here, in case it proves useful to others: (N.B. Requires .NET 4.5)

    It looks like using System.IO.Compression.ZipArchive in combination with System.IO.Memorystream is the way forward. I've got this now:

    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
    }
    

    Which does exactly what I need.

    Thanks all :)

提交回复
热议问题