1. 程式人生 > >[轉][C#]壓縮解壓

[轉][C#]壓縮解壓

{
    internal static class Compressor
    {
        public static Stream Decompress(Stream source, bool bidiStream)
        {
            int arg_1E_0 = source.ReadByte();
            int num = source.ReadByte();
            source.Position -= 2L;
            if (arg_1E_0 != 31 || num != 139)
            {
                
return null; } if (bidiStream) { Stream stream = new MemoryStream(); using (GZipStream gZipStream = new GZipStream(source, CompressionMode.Decompress)) { byte[] buffer = new byte[4096];
while (true) { int num2 = gZipStream.Read(buffer, 0, 4096); if (num2 == 0) { break; } stream.Write(buffer, 0, num2); } } stream.Position
= 0L; return stream; } return new GZipStream(source, CompressionMode.Decompress); } public static Stream Compress(Stream dest) { return new GZipStream(dest, CompressionMode.Compress, true); } public static byte[] Compress(byte[] buffer) { byte[] result; using (MemoryStream memoryStream = new MemoryStream()) { using (Stream stream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { stream.Write(buffer, 0, buffer.Length); } result = memoryStream.ToArray(); } return result; } public static string Compress(string source) { return Convert.ToBase64String(Compressor.Compress(new UTF8Encoding().GetBytes(source))); } public static byte[] Decompress(byte[] buffer) { byte[] result; using (MemoryStream memoryStream = new MemoryStream(buffer)) { if (Compressor.IsStreamCompressed(memoryStream)) { using (MemoryStream memoryStream2 = Compressor.Decompress(memoryStream, true) as MemoryStream) { result = memoryStream2.ToArray(); return result; } } result = buffer; } return result; } public static string Decompress(string source) { byte[] bytes = Compressor.Decompress(Convert.FromBase64String(source)); return new UTF8Encoding().GetString(bytes); } public static bool IsStreamCompressed(Stream stream) { int arg_1E_0 = stream.ReadByte(); int num = stream.ReadByte(); stream.Position -= 2L; return arg_1E_0 == 31 && num == 139; } } }

程式碼來自:https://github.com/FastReports/