【代码块】-Helper-GZIP
整理代码块
代码块整理后存储,供后期使用
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
public class GzipHelper
{
/// <summary>
/// GZIP解压缩
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
protected byte[] Decompress(byte[] data)
{
try
{
var ms = new MemoryStream(data);
var zip = new GZipStream(ms, CompressionMode.Decompress, true);
var msreader = new MemoryStream();
var buffer = new byte[0x1000];
while (true)
{
var reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}
msreader.Write(buffer, 0, reader);
}
zip.Close();
ms.Close();
msreader.Position = 0;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception)
{
throw;
}
}
protected string Decompress(Stream stream)
{
try
{
var zip = new GZipStream(stream, CompressionMode.Decompress, true);
var msreader = new MemoryStream();
var buffer = new byte[0x1000];
while (true)
{
var reader = zip.Read(buffer, 0, buffer.Length);
if (reader <= 0)
{
break;
}
msreader.Write(buffer, 0, reader);
}
zip.Close();
stream.Close();
msreader.Position = 0;
buffer = msreader.ToArray();
msreader.Close();
return Encoding.GetEncoding("UTF-8").GetString(buffer);
}
catch (Exception)
{
throw;
}
}
}
如果有错误的地方,还望各位多多指点
写个博客,来记录自己成长的一些经历,或许也能顺便帮助他人。
由于使用GitHub仓库作为图床,会有图片显示不出来的情况。