// 打包并添加到附件
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, , buffer.Length); entry = new ZipEntry("test\\test2.html");
entry.IsUnicodeText = true;
entry.DateTime = DateTime.Now;
zipStream.IsStreamOwner = false;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, , buffer.Length); zipStream.Finish();
zipStream.Close();
ms.Seek(, 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 != || num != )
{
return null;
}
if (bidiStream)
{
Stream stream = new MemoryStream();
using (GZipStream gZipStream = new GZipStream(source, CompressionMode.Decompress))
{
byte[] buffer = new byte[];
while (true)
{
int num2 = gZipStream.Read(buffer, , );
if (num2 == )
{
break;
}
stream.Write(buffer, , 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, , 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 == && num == ;
}
}
}

代码来自:https://github.com/FastReports/

[转][C#]压缩解压的更多相关文章

  1. Linux 压缩解压

    压缩解压 ------------------------------------------ linux 下所有的压缩格式,WinRAR 都支持 gzip .gz 格式 压缩文件: gzip 文件名 ...

  2. SAPCAR 压缩解压软件的使用方法

    SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...

  3. 对称加密之AES、压缩解压以及压缩加密解密解压综合实战

    AES 压缩解压 压缩加密解密解压 对称加密: 就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密.密钥是控制加密及解密过程的指令.算法是一组规则,规定如何进行加密和解密.   因此加密的安 ...

  4. linux笔记:linux常用命令-压缩解压命令

    压缩解压命令:gzip(压缩文件,不保留原文件.这个命令不能压缩目录) 压缩解压命令:gunzip(解压.gz的压缩文件) 压缩解压命令:tar(打包压缩目录或者解压压缩文件.打包的意思是把目录打包成 ...

  5. .NET使用ICSharpCode.SharpZipLib压缩/解压文件

    SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...

  6. linux驱动系列之文件压缩解压小节(转)

    转至网页:http://www.jb51.net/LINUXjishu/43356.html Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通 ...

  7. Linux的压缩解压命令快速上手——解压篇

    在Linux系统中,压缩文件通常是先将若干文件(包括目录)打包成一个tar文件,然后再调用压缩程序将tar文件压缩成相应的压缩包,这也就是为什么Linux系的压缩包的后缀通常都是像tar.gz,tar ...

  8. RAR压缩解压命令

    RAR压缩解压命令 这几天一直没空更新博客,现在补上: 先介绍一下rar的命令格式及相关参数含义(摘自rar): 用法:   rar <命令> -<开关 1> -<开关 ...

  9. 使用SevenZipSharp压缩/解压7z格式

    7z格式采用的LZMA算法,号称具有现今最高压缩率.笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压.不得不说,SevenZipSharp的API设计得非常 ...

  10. huffman压缩解压文件【代码】

    距离上次写完哈夫曼编码已经过去一周了,这一周都在写huffman压缩解压,哎,在很多小错误上浪费了很多时间调bug.其实这个程序的最关键部分不是我自己想的,而是借鉴了某位园友的代码,但是,无论如何,自 ...

随机推荐

  1. [poj P2411] Mondriaan's Dream

    [poj P2411] Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18023   A ...

  2. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)

    这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...

  3. C++获取网络数据

    1.  获取数据 工具libcurl libcurl主要功能就是用不同的协议连接和沟通不同的服务器.libcurl当前支持http, https, ftp, gopher, telnet, dict, ...

  4. liunx文件操作 文件压缩

    文件备份和压缩命令 在Linux中,常用的文件压缩工具有gzip,bzip2,zip. 'bzip2'是最理想的压缩工具,它提供了最大限度的压缩. 'zip'兼容好,windows也支持. bzip2 ...

  5. Unity调试模式设置辅助线是否可见

    1.新建变量 //调试的标识(状态开关) public bool m_debug = true; 2.在画线方法中写 //如果非调试状态,则不再输出网格和立方体 if (!m_debug) { ret ...

  6. MySQL-8.0.15在Win10和Ubuntu上安装&使用

    一.Windows环境下安装: 1.下载MySQL压缩包 官网地址:https://dev.mysql.com/downloads/mysql/ 点击直接下载: 2.解压到本地目录,并添加一个配置文件 ...

  7. 剑指Offer 48. 不用加减乘除做加法 (其他)

    题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 题目地址 https://www.nowcoder.com/practice/59ac416b4b944300 ...

  8. Windows跨域远程连接防火墙设置

    按照正常的防火墙的设置,发现跨域远程依然不行,后来进过排除法发现 还需要打开icmpv4所有的协议,才可以

  9. s21day07 python笔记

    s21day07 python笔记 一.昨日内容回顾及补充 回顾 补充 将前面所提到的功能,统一改称为方法 二.深浅拷贝 基本格式 v1 = [1,2,3] import copy v2 = copy ...

  10. 软件工程 week 03

    一.效能分析 1.作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2139 2.git地址:https://git.coding.ne ...