.NET GZipStream 压缩和解压缩
下面这段代码有什么问题。我总是得到 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;
}
如果您能指出我在哪里犯了错误,我将不胜感激。
What is wrong with this code below. I always get FALSE, meaning after compression, decompressed data does not match original value.
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;
}
I will really appreciate if you can point out where I am making a mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
Write
调用后关闭GZipStream
。如果不调用
Close
,某些数据可能会被缓冲但尚未写入底层流。Close the
GZipStream
after theWrite
call.Without calling
Close
, there's a possibility that some data is buffered and is not written to the underlying stream yet.试试这个代码:
问题是您没有使用 ASCIIEncoder 来获取样本数据的字符串。
编辑:这是代码的清理版本,以帮助关闭/处置:
Try this code:
The problem what that you were not using the ASCIIEncoder to get the string back for sampleData.
EDIT: Here's a cleaned up version of the code to help with Closing/Disposing:
解决这个问题需要三个问题。
1. WRITE GZipStream 需要关闭后 :: hgs.Close();
GZipStream 读取需要使用 WHILE 循环,并将未压缩数据的较小缓冲区写入 MemoryStream :: outStream.Write( ... );
解压后的byte[]数组的转换需要使用编码转换:: string sampleOut = encoding.GetString(data);
这是最终的代码:-
我在使用 Microsoft .NET GZipStream 对象进行压缩/解压缩工作时遇到了很大的麻烦。最后,我认为我以正确的方式得到了它。非常感谢大家,因为解决方案来自你们所有人。
There were three issues to solve the problem.
1. After WRITE GZipStream NEEDED to be closed :: hgs.Close();
GZipStream read needed to be used a WHILE loop and writing the smaller buffer of uncompressed data to a MemoryStream :: outStream.Write( ... );
The converting of decompressed byte[] array needed to use encoding conversion :: string sampleOut = encoding.GetString(data);
Here is the final code:-
I had really trouble to get compress/decompress work with Microsoft .NET GZipStream object. Finally, I think I got it in right way. many thanks to all as the solution came from all of you.
这是我的最终解决方案的清理版本:
Here's my cleaned up version of the final solution: