zoukankan      html  css  js  c++  java
  • [转][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/

  • 相关阅读:
    【12】python模块:itsdangerous(生成临时身份令牌)
    python作业/练习/实战:下载QQ群所有人的头像
    【4】Python操作redis
    【7】Python网络请求:requests模块
    【6】Python网络请求:urllib模块
    python学习笔记:目录结构
    【9】Python接口开发:flask Demo实例
    【8】Python接口开发:PythonWEB框架之Flask
    前端学习笔记——引入css文件、样式优先级
    Yii2模型介绍
  • 原文地址:https://www.cnblogs.com/z5337/p/10219749.html
Copyright © 2011-2022 走看看