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

Read blob zip file directly without writing it to disk

我正在使用 ODP.net 和 Powershell 来获取 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()
 }

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

您可以使用 Ionic zip 来完成。它可以从流中读取 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