.NET GZipStream 压缩和解压
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1590846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET GZipStream compress and decompress
提问by MehdiAnis
What is wrong with this code below. I always get FALSE, meaning after compression, decompressed data does not match original value.
下面这段代码有什么问题。我总是得到 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;
}
I will really appreciate if you can point out where I am making a mistake.
如果您能指出我哪里出错了,我将不胜感激。
回答by Mehrdad Afshari
Close the GZipStreamafter the Writecall.
调用GZipStream后关闭Write。
Without calling Close, there's a possibility that some data is buffered and is not written to the underlying stream yet.
如果不调用Close,则可能会缓冲某些数据并且尚未将其写入底层流。
回答by Jason Evans
Try this code:
试试这个代码:
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 = new MemoryStream();
GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress);
hgs.Write(data, 0, data.Length);
byte[] cmpData = cmpStream.ToArray();
MemoryStream decomStream = new MemoryStream(cmpData);
hgs = new GZipStream(decomStream, CompressionMode.Decompress);
hgs.Read(data, 0, data.Length);
string sampleOut = encoding.GetString(data);
result = String.Equals(sample, sampleOut);
return result;
}
The problem what that you were not using the ASCIIEncoder to get the string back for sampleData.
问题是您没有使用 ASCIIEncoder 为 sampleData 取回字符串。
EDIT: Here's a cleaned up version of the code to help with Closing/Disposing:
编辑:这是代码的清理版本,以帮助关闭/处理:
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);
// Compress.
GZipStream hgs;
byte[] cmpData;
using(MemoryStream cmpStream = new MemoryStream())
using(hgs = new GZipStream(cmpStream, CompressionMode.Compress))
{
hgs.Write(data, 0, data.Length);
hgs.Close()
// Do this AFTER the stream is closed which sounds counter intuitive
// but if you do it before the stream will not be flushed
// (even if you call flush which has a null implementation).
cmpData = cmpStream.ToArray();
}
using(MemoryStream decomStream = new MemoryStream(cmpData))
using(hgs = new GZipStream(decomStream, CompressionMode.Decompress))
{
hgs.Read(data, 0, data.Length);
}
string sampleOut = encoding.GetString(data);
bool result = String.Equals(sample, sampleOut);
return result;
}
回答by MehdiAnis
There were three issues to solve the problem. 1. After WRITE GZipStream NEEDED to be closed :: hgs.Close();
有三个问题来解决这个问题。1. 写入 GZipStream 后需要关闭 :: 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);
GZipStream 读取需要使用 WHILE 循环并将未压缩数据的较小缓冲区写入 MemoryStream :: outStream.Write( ... );
解压后的byte[]数组的转换需要使用编码转换:: string sampleOut = encoding.GetString(data);
Here is the final code:-
这是最终的代码:-
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 = new MemoryStream();
GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress, true);
hgs.Write(data, 0, data.Length);
hgs.Close();
//DeCompress
byte[] cmpData = cmpStream.ToArray();
MemoryStream decomStream = new MemoryStream(cmpData);
data = new byte[data.Length];
hgs = new GZipStream(decomStream, CompressionMode.Decompress, true);
byte[] step = new byte[16]; //Instead of 16 can put any 2^x
MemoryStream outStream = new MemoryStream();
int readCount;
do
{
readCount = hgs.Read(step, 0, step.Length);
outStream.Write(step, 0, readCount);
} while (readCount > 0);
hgs.Close();
string sampleOut = encoding.GetString(outStream.ToArray());
result = String.Equals(sample, sampleOut);
return result;
}
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.
我真的很难使用 Microsoft .NET GZipStream 对象进行压缩/解压缩工作。最后,我认为我以正确的方式得到了它。非常感谢所有人,因为解决方案来自你们所有人。
回答by oPJo
Here's my cleaned up version of the final solution:
这是我的最终解决方案的清理版本:
[Test]
public void Test_zipping_with_memorystream()
{
const string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes(sample);
string sampleOut;
byte[] cmpData;
// Compress
using (var cmpStream = new MemoryStream())
{
using (var hgs = new GZipStream(cmpStream, CompressionMode.Compress))
{
hgs.Write(data, 0, data.Length);
}
cmpData = cmpStream.ToArray();
}
using (var decomStream = new MemoryStream(cmpData))
{
using (var hgs = new GZipStream(decomStream, CompressionMode.Decompress))
{
using (var reader = new StreamReader(hgs))
{
sampleOut = reader.ReadToEnd();
}
}
}
Assert.IsNotNullOrEmpty(sampleOut);
Assert.AreEqual(sample, sampleOut);
}

