我有这种解压缩方法,我想基于它进行压缩。谁能帮助我? (我试着自己写但不起作用)
解压缩方法:
public static byte[] Decompress(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(data, 0, data.Length);
memoryStream.Position = 0L;
GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress, true);
MemoryStream memoryStream2 = new MemoryStream();
byte[] array = new byte[64];
for (int i = gZipStream.Read(array, 0, array.Length); i > 0; i = gZipStream.Read(array, 0, array.Length))
{
memoryStream2.Write(array, 0, i);
}
gZipStream.Close();
return memoryStream2.ToArray();
}
这就是我尝试编写Compress方法的方法,但是当我将其解压缩时没有得到正确的结果:
public static byte[] Compress(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(data, 0, data.Length);
memoryStream.Position = 0L;
GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, false);
byte[] array = new byte[64];
gZipStream.Write(array, 0, array.Length);
gZipStream.Close();
return memoryStream.ToArray();
}
答案 0 :(得分:1)
尝试使用以下简单实现:
public static byte[] Compress(byte[] data)
{
using (MemoryStream memoryStream = new MemoryStream())
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gzipStream.Write(data, 0, data.Length);
return memoryStream.ToArray();
}
}