oracle C#二进制编写器,使用8位写入

rkttyhzu  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(208)

我正在使用Binarywriter将blob数据(PDF)从oracle保存到磁盘。当我打开生成的文件时,结果很难看。我认为这是1个字符用一个字节写入的问题。
我如何才能增加到8。(/BitsPerComponent 8)
有什么想法吗?

long CurrentIndex = 0;
int BufferSize = 10000;
long BytesReturned;
byte[] Blob = new byte[BufferSize];

OracleDataReader reader = comando.ExecuteReader(CommandBehavior.SequentialAccess);
string filepath = "C:\\ttttt.pdf";
while (reader.Read())
{
    FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
    BinaryWriter writer = new BinaryWriter(fs);
    //reset the index to the beginning of the file
    CurrentIndex = 0;
    BytesReturned = reader.GetBytes(0, CurrentIndex, Blob, 0, BufferSize);
    while (BytesReturned == BufferSize)
    {
        writer.Write(Blob);
        writer.Flush();
        CurrentIndex += BufferSize;
        BytesReturned = reader.GetBytes(0, CurrentIndex, Blob, 0, BufferSize);
    }

    writer.Write(Blob, 0, (int)BytesReturned);
    writer.Flush();
    writer.Close();
    fs.Close();
}
aor9mmx1

aor9mmx11#

您不需要使用BinaryWriter。直接写入数据流即可。BinaryWriter的预期使用案例是将基本资料类型写入数据流以进行序列化。

**更新:**自动从Base64(MD5(Blob))生成文件名.

long CurrentIndex = 0;
int BufferSize = 10000;
long BytesReturned;
byte[] Blob = new byte[BufferSize];

using (var hasher = MD5.Create())
{
    using (var reader = comando.ExecuteReader(CommandBehavior.SequentialAccess))
    {
        while (reader.Read())
        {
            var filename = Convert.ToBase64String(hasher.ComputeHash(Blob)).Replace("=", string.Empty);
            var filepath = Path.ChangeExtension(Path.Combine("C:\\", filename), ".pdf");

            using (var fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                //reset the index to the beginning of the file
                CurrentIndex = 0;
                BytesReturned = reader.GetBytes(0, CurrentIndex, Blob, 0, BufferSize);
                while (BytesReturned == BufferSize)
                {
                    fs.Write(Blob, 0, Blob.Length);
                    CurrentIndex += BufferSize;
                    BytesReturned = reader.GetBytes(0, CurrentIndex, Blob, 0, BufferSize);
                }

                fs.Write(Blob, 0, (int)BytesReturned);
            }
        }
    }
}

相关问题