gpt4 book ai didi

C# 在 Base64 中解码大型/巨大的 XML/CDATA 上下文

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:00 25 4
gpt4 key购买 nike

我得到了一个非常大的 XML (>1GB),其中包含一个包含 76 个字符的 base64 和 CRLF 的 CDATA 内容,最后一个内容用“=”填充,没有CRLF.

获得 base64 解码的最自然方法是使用 XmlReader reader.ReadElementContentAsBase64(...) 但 thay 在 100Mb 时停止工作。 (=OOM)

我在搜索流式传输到 Base64 转换的方法后发现了这一点。

string Original = "foo bar, this is an example";

byte[] ToBase64; string Decoded;

using ( MemoryStream ms = new MemoryStream() ) using ( CryptoStream cs = new CryptoStream( ms, new ToBase64Transform(),
CryptoStreamMode.Write ) ) using ( StreamWriter st = new StreamWriter( cs ) ) {
st.Write( Original );
st.Flush();
ToBase64 = ms.ToArray(); }

using ( MemoryStream ms = new MemoryStream( ToBase64 ) ) using ( CryptoStream cs = new CryptoStream( ms, new FromBase64Transform(),
CryptoStreamMode.Read ) ) using ( StreamReader sr = new StreamReader( cs ) ) {
Decoded = sr.ReadToEnd(); }

Console.WriteLine( Original ); Console.WriteLine( Encoding.Default.GetString( ToBase64 ) ); Console.WriteLine( Decoded );

此示例有一个字符串作为输入,但我需要转换此代码以处理文件并从文件中的某个位置开始/停止读取。

              CData Start                         CDataEnd
| |
V V

[xml..[Envelope]...[Body]base64 with 76 char + CRLF + padding "="...[/Body]...[/Envelope]

最佳答案

这是我能创造的最好的.. int readbytes = 0;

                long bytesToRead = (CDataEnd - CDataStart);
using (CryptoStream cryptoStremFromBase64 = new CryptoStream(context.OutStream, new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces), CryptoStreamMode.Write))
{


byte[] bytebuffer = new byte[bytesToRead % 10485760];
readbytes = context.InStream.Read(bytebuffer, 0, bytebuffer.Length);
cryptoStremFromBase64.Write(bytebuffer, 0, bytebuffer.Length);

while (context.InStream.Position < CDataEnd)
{
WriteToLog("Context.Instream.Position = " + context.InStream.Position.ToString(), context, startingTime);
byte[] bytebuffer2 = new byte[10485760];
readbytes = context.InStream.Read(bytebuffer2, 0, bytebuffer2.Length);
cryptoStremFromBase64.Write(bytebuffer2, 0, bytebuffer2.Length);
}


cryptoStremFromBase64.Flush();

}

Context.InStream 是 base64 的 SOAPcontext.Outstream是body从base64转换后的输出

有什么办法可以改进吗?

关于C# 在 Base64 中解码大型/巨大的 XML/CDATA 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com