You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.6 KiB
55 lines
1.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
|
|
namespace Apewer.Internals
|
|
{
|
|
|
|
internal class GzipHelper
|
|
{
|
|
|
|
/// <summary>对数据进行 GZip 压缩。</summary>
|
|
public static byte[] ToGzip(byte[] bytes)
|
|
{
|
|
if (bytes == null) return Constant.EmptyBytes;
|
|
if (bytes.Length == 0) return Constant.EmptyBytes;
|
|
|
|
byte[] result;
|
|
var output = new MemoryStream();
|
|
var zip = new GZipStream(output, CompressionMode.Compress, true);
|
|
zip.Write(bytes, 0, bytes.Length);
|
|
zip.Close();
|
|
zip.Dispose();
|
|
result = output.ToArray();
|
|
output.Close();
|
|
output.Dispose();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>对数据进行 GZip 解压。</summary>
|
|
public static byte[] FromGzip(byte[] bytes)
|
|
{
|
|
if (bytes == null) return Constant.EmptyBytes;
|
|
if (bytes.Length == 0) return Constant.EmptyBytes;
|
|
|
|
byte[] result;
|
|
var input = new MemoryStream(bytes);
|
|
var output = new MemoryStream();
|
|
input.Position = 0;
|
|
var zip = new GZipStream(input, CompressionMode.Decompress, true);
|
|
StreamHelper.Read(zip, output, 64);
|
|
result = output.ToArray();
|
|
zip.Close();
|
|
zip.Dispose();
|
|
input.Close();
|
|
input.Dispose();
|
|
output.Close();
|
|
output.Dispose();
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|