通过BinaryReader将字节数组解压缩为字符串,产生空字符串
我试图解压缩一个字节数组,并使用二进制阅读器将其转换为字符串。 当执行以下代码时,inStream位置从0更改为数组的长度,但str始终为空字符串。
BinaryReader br = null; string str = String.Empty; using (MemoryStream inStream = new MemoryStream(pByteArray)) { GZipStream zipStream = new GZipStream(inStream, CompressionMode.Decompress); BinaryReader br = new BinaryReader(zipStream); str = br.ReadString(); inStream.Close(); br.Close(); }
您还没有展示如何压缩数据,但这里是压缩和解压缩缓冲区的完整示例:
上述就是C#学习教程:通过BinaryReader将字节数组解压缩为字符串,产生空字符串分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
using System; using System.IO; using System.IO.Compression; using System.Text; class Program { static void Main() { var test = "foo bar baz"; var compressed = Compress(Encoding.UTF8.GetBytes(test)); var decompressed = Decompress(compressed); Console.WriteLine(Encoding.UTF8.GetString(decompressed)); } static byte[] Compress(byte[] data) { using (var compressedStream = new MemoryStream()) using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) { zipStream.Write(data, 0, data.Length); zipStream.Close(); return compressedStream.ToArray(); } } static byte[] Decompress(byte[] data) { using (var compressedStream = new MemoryStream(data)) using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) using (var resultStream = new MemoryStream()) { zipStream.CopyTo(resultStream); return resultStream.ToArray(); } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/950160.html