将DeflateStream结果有效地存储到`byte[]`的方法?

huangapple go评论64阅读模式
英文:

Efficient way to store the DeflateStream result to `byte[]`?

问题

我正在将 SQL 服务器表中的 varbinary(MAX) 读入 byte[] content 变量中。然后使用 DeflateStream 解压缩内容。最终目标是将解压缩的内容分配给另一个 byte[] 数组,然后将其存储到 SQL 表中。

问题的核心是如何分配输出的 byte[] 数组以及如何将解压缩的结果分配给它。到目前为止,我正在使用另一个 MemoryStream 和其 .ToArray() 方法来分配结果,就像这样:

byte[] content = row.Field<byte[]>("Content");

using (var memstream = new MemoryStream(content, 0, content.Length))
using (var defstream = new DeflateStream(memstream, CompressionMode.Decompress))
using (var outstream = new MemoryStream())
{
    defstream.CopyTo(outstream);
    byte[] deflated = outstream.ToArray();

    // ... 将压缩后的结果写入 SQL 表中。
}

这是一个合理高效的解决方案吗?还是有更清晰的方法将 DeflateStream 的结果保存到 byte[] 中?

我知道 DeflateStream 实现了 .Read(buffer, 0, bufferSize)。然而,结果的大小事先是未知的。

上下文: 压缩的内容是 JPEG 或 PNG 格式的产品图像。因此,这个图像会再次被 Microsoft 压缩。

英文:

I am reading the varbinary(MAX) from the SQL server table into the byte[] content variable. Then the DeflateStream is used to unpack the content. The final goal is to assign the unpacked content to another byte[] array, and then store it into the SQL table.

The core of the question is how to allocate the output byte[] array and how to assign the result of deflating into it. So far, I am using another MemoryStream and its .ToArray() method to assign result, like this:

    byte[] content = row.Field&lt;byte[]&gt;(&quot;Content&quot;);

    using (var memstream = new MemoryStream(content, 0, content.Length))
    using (var defstream = new DeflateStream(memstream, CompressionMode.Decompress))
    using (var outstream = new MemoryStream())
    {
        defstream.CopyTo(outstream);
        byte[] deflated = outstream.ToArray();

        // ... write the deflated result to the SQL table.
    }

Is that a reasonably efficient solution? Or is there a cleaner way to save the DeflateStream result to byte[]?

I know that the DeflateStream implements the .Read(buffer, 0, bufferSize). However, the size of the result is not known in advance.

Context: The compressed content is a product image in JPEG or PNG. For the reason, the image is compressed once again by Microsoft.

答案1

得分: 1

只需将流直接传递给您的命令参数的 Value

注意: 在命令执行之前,请不要使用 using 处理流。

byte[] content = row.Field<byte[]>("Content");

using (var memstream = new MemoryStream(content))
using (var defstream = new DeflateStream(memstream, CompressionMode.Decompress))
{
    command.Parameters.Add("@yourVarBinary", SqlDbType.VarBinary, -1).Value = defstream;
    command.ExecuteNonQuery.....
}

假设 row.Field 实际上是一个 SqlDataReader,您可以在两个方向上这样做。

using (var stream = row.GetStream(row.GetOrdinal("Content")))
using (var defstream = new DeflateStream(stream, CompressionMode.Decompress))
{
    command.Parameters.Add("@yourVarBinary", SqlDbType.VarBinary, -1).Value = defstream;
    command.ExecuteNonQuery.....
}

另请参阅文档

英文:

You can just pass the stream directly as the Value for your command's parameter.

Note: do not dispose the stream with using until the command has been executed.

byte[] content = row.Field&lt;byte[]&gt;(&quot;Content&quot;);

using (var memstream = new MemoryStream(content))
using (var defstream = new DeflateStream(memstream, CompressionMode.Decompress))
{
    command.Parameters.Add(&quot;@yourVarBinary&quot;, SqlDbType.VarBinary, -1).Value = defstream;
    command.ExecuteNonQuery.....
}

Assuming row.Field is actually a SqlDataReader, you can do this in both directions.

using (var stream = row.GetStream(row.GetOrdinal(&quot;Content&quot;)))
using (var defstream = new DeflateStream(stream, CompressionMode.Decompress))
{
    command.Parameters.Add(&quot;@yourVarBinary&quot;, SqlDbType.VarBinary, -1).Value = defstream;
    command.ExecuteNonQuery.....
}

See also the documentation.

huangapple
  • 本文由 发表于 2023年7月20日 19:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定