有没有办法使用 Powershell 操作内存中的 zip 文件内容? [英] Is there a way of manipulating zip file contents in memory with Powershell?

查看:39
本文介绍了有没有办法使用 Powershell 操作内存中的 zip 文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 Powershell remoting with the Lync Cmdlets, the -AsBytes flag is the only way to work the Export-CsConfiguration cmdlet, and it returns a Byte array, which, if you write it to disk with "Set-Content -Encoding Byte", results in a zip file.

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

I'm wondering if there's a way to expand the content of the byte array into the two files which are contained in that zip, but only do it in memory. I'm not really interested in keeping the zip file around for long, as it changes frequently, and something about writing the file contents out to disk only to read them straight back in again so I can do something with the uncompressed content seems horribly wrong to me.

那么有没有办法避免写入磁盘:

So is there someway of doing something like this which avoids writes to disk:

$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)

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

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

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
}

这正是我需要的.

谢谢大家:)

这篇关于有没有办法使用 Powershell 操作内存中的 zip 文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆