直接读取blob zip文件而不将其写入磁盘

时间:2016-06-09 09:45:42

标签: powershell blob odp.net

我正在使用带有Powershell的ODP.net获取blob压缩文件。

[void][System.Reflection.Assembly]::LoadFile("C:\DLL\Oracle.ManagedDataAccess.dll")
$OracleConnexion = New-Object Oracle.ManagedDataAccess.Client.OracleConnection('User Id=test;Password="test";Data Source=10.2.2.1/TEST')

$OracleConnexion.Open()
$Query=$OracleConnexion.CreateCommand()
$Query.CommandText="SELECT BLOB from MyTable Where ID=01"
$ExecuteQuery=$Query.ExecuteReader()

$Path = "C:\temp"

while ($ExecuteQuery.Read()){

   $Localfile = New-Object IO.FileStream("$($Path)\$($ExecuteQuery["LOG_ID"]).zip",[IO.FileMode]::Create)
   $Localfile.Write($ExecuteQuery["XML_TRACE"],0,$ExecuteQuery["XML_TRACE"].Length)
   $Localfile.Close()

   $Zip = [io.compression.zipfile]::OpenRead("$($Path)\$($Executequery["LOG_ID"]).zip")
   $Stream = $Zip.Entries.Open()
   $Reader = New-Object IO.StreamReader($stream)
   $XML = $Reader.ReadToEnd()

   $Reader.Close()
   $Stream.Close()
   $Zip.Dispose()

    }

正如您所看到的,首先我使用$Localfile.Write将文件写入磁盘,然后使用[io.compression.zipfile]::OpenRead我正在读取压缩文件的内容。

我的代码有效,但我想直接将我的blob作为zip文件读取而不将其写入磁盘,如下所示:

  while ($ExecuteQuery.Read()){

   $Zip = [io.compression.zipfile]::OpenRead($ExecuteQuery["XML_TRACE"]).zip)
   $Stream = $Zip.Entries.Open()
   $Reader = New-Object IO.StreamReader($stream)
   $XML = $Reader.ReadToEnd()
   $XML

   $Reader.Close()
   $Stream.Close()
   $Zip.Dispose()

    }

编辑:它适用于离子!

   while ($ExecuteRequete.Read()){
$ZipStream = New-Object System.IO.Memorystream
$ZipStream.Write($ExecuteRequete["XML_TRACE"],0,$ExecuteRequete["XML_TRACE"].Length)
$ZipStream.Position = 0
$Zip = [Ionic.Zip.ZipFile]::Read($ZipStream)

$Stream = New-Object IO.MemoryStream
$Zip.Extract($Stream)
$stream.Position = 0

$Reader = New-Object IO.StreamReader($stream)
$XML = $Reader.ReadToEnd()

$Reader.Close()
$Stream.Close()
$ZipStream.Dispose()
$Zip.Dispose()
 }

1 个答案:

答案 0 :(得分:2)

您无法使用IO.Compression.Zipfile执行该操作,有关所有可用方法,请参阅https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile_methods(v=vs.110).aspx

你可以用Ionic拉链做到这一点。它可以从流中读取zip:

clear
Add-Type -Path "E:\sw\NuGet\Packages\DotNetZip.1.9.7\lib\net20\Ionic.Zip.dll"
$zip = [Ionic.Zip.ZipFile]::Read($stream)

$file = $zip | where-object { $_.FileName -eq "XMLSchema1.xsd"}

$stream = new-object IO.MemoryStream
$file.Extract($stream)
$stream.Position = 0

$reader = New-Object IO.StreamReader($stream)
$text = $reader.ReadToEnd()
$text

$reader.Close()
$stream.Close()
$zip.Dispose()

这里是文档:http://dotnetzip.herobo.com/DNZHelp/Index.html