[转][C#]压缩解压

// 打包并添加到附件
using (MemoryStream ms = new MemoryStream())
{
    using (ZipOutputStream zipStream = new ZipOutputStream(ms))
    {
        var buffer = Encoding.UTF8.GetBytes(sb.ToString());
        ZipEntry entry = new ZipEntry("test.html");
        entry.IsUnicodeText = true;
        entry.DateTime = DateTime.Now;
        zipStream.IsStreamOwner = false;
        zipStream.PutNextEntry(entry);
        zipStream.Write(buffer, 0, buffer.Length);

        entry = new ZipEntry("test\\test2.html");
        entry.IsUnicodeText = true;
        entry.DateTime = DateTime.Now;
        zipStream.IsStreamOwner = false;
        zipStream.PutNextEntry(entry);
        zipStream.Write(buffer, 0, buffer.Length);

        zipStream.Finish();
        zipStream.Close();
        ms.Seek(0, SeekOrigin.Begin);
        message.Attachments.AddFileAttachment("差异test.zip", ms.ToArray());
    }
}

 可参考:https://www.jb51.net/article/131706.htm

{
    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/

posted on 2019-01-04 14:40  z5337  阅读(214)  评论(0编辑  收藏  举报