gpt4 book ai didi

.NET GZipStream 压缩和解压

转载 作者:行者123 更新时间:2023-12-03 22:49:04 28 4
gpt4 key购买 nike

下面这段代码有什么问题。我总是得到 FALSE,意思是压缩后,解压后的数据与原始值不匹配。

public static bool Test()
{
string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = encoding.GetBytes(sample);
bool result = false;

//Compress
MemoryStream cmpStream;
cmpStream = new MemoryStream();
GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress);
hgs.Write(data, 0, data.Length);
byte[] cmpData = cmpStream.ToArray();

MemoryStream decomStream;
decomStream = new MemoryStream(cmpData);
hgs = new GZipStream(decomStream, CompressionMode.Decompress);
hgs.Read(data, 0, data.Length);

string sampleOut = System.BitConverter.ToString(data);

result = String.Equals(sample, sampleOut) ;
return result;
}

如果您能指出我哪里出错了,我将不胜感激。

最佳答案

关闭 GZipStreamWrite称呼。

无需调用 Close ,有可能某些数据已被缓冲并且尚未写入底层流。

关于.NET GZipStream 压缩和解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1590846/

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