WPF RichTextBox转化为RTF格式,并进行压缩、解压
来源:互联网 发布:虎扑认证的淘宝店 编辑:程序博客网 时间:2024/04/28 09:27
关于System.IO.Compression.DeflateStream,msdn上有一句描述如下:
此类表示 Deflate 算法,这是无损压缩和解压缩文件的行业标准算法。它结合了 LZ77 算法和霍夫曼编码。只能使用以前绑定的中间存储量来产生或使用数据,即使对于任意长度的、按顺序出现的输入数据流也是如此。这种格式可以通过不涉及专利使用权的方式轻松实现。有关更多信息,请参见 RFC 1951。"DEFLATE Compressed Data Format Specification version 1.3(DEFLATE 压缩数据格式规范版本 1.3)。"此类不能用于压缩大于 4 GB 的文件。
此类原本并不提供用来向 .zip 存档中添加文件或从 .zip 存档中提取文件的功能。(原链接)
1:存储和导入rtf的代码:
代码
public static class RichTextBoxEx
{
public static string RTF(this RichTextBox richTextBox)
{
string rtf = string.Empty;
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
textRange.Save(ms, System.Windows.DataFormats.Rtf);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
rtf = sr.ReadToEnd();
}
return rtf;
}
public static void LoadFromRTF(this RichTextBox richTextBox, string rtf)
{
if (string.IsNullOrEmpty(rtf))
{
throw new ArgumentNullException();
}
TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write(rtf);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
textRange.Load(ms, DataFormats.Rtf);
}
}
}
}
2:压缩的简单封装,转化为二进制,以及二进制转化为Base64String等::
代码
public class StringCompress
{
public static string Decompress(byte[] bys)
{
return Decompress(Convert.ToBase64String(bys));
}
public static string Decompress(string strSource)
{
return Decompress(strSource, (3 * 1024 * 1024 + 256));//字符串不会超过3M
}
public static string Decompress(string strSource, int length)
{
byte[] buffer = Convert.FromBase64String(strSource);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress);
stream.Flush();
int nSize = length;
byte[] decompressBuffer = new byte[nSize];
int nSizeIncept = stream.Read(decompressBuffer, 0, nSize);
stream.Close();
return System.Text.Encoding.Unicode.GetString(decompressBuffer, 0, nSizeIncept);//转换为普通的字符串
}
public static byte[] Compress(string strSource)
{
if (strSource == null)
throw new System.ArgumentException("字符串为空!");
System.Text.Encoding encoding = System.Text.Encoding.Unicode;
byte[] buffer = encoding.GetBytes(strSource);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress, true);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
buffer = ms.ToArray();
ms.Close();
return buffer;
// return Convert.ToBase64String(buffer); //将压缩后的byte[]转换为Base64String
}
}
- WPF RichTextBox转化为RTF格式,并进行压缩、解压
- WPF RichTextBox转化为rtf格式,以及加载,压缩
- rtf格式赋值给WPF RICHTEXTBOX
- 字符串转化为RTF格式-RTF研究系列
- WPF RichTextBox和rtf 复制等操作
- C# Visual Studio 2005中RichTextBox保存文件为rtf格式
- C# Visual Studio 2005中RichTextBox打开文件为rtf格式
- RichTextBox中的RTF格式的读取和保存
- RichTextBox读取Doc、txt、rtf三种格式
- SVG与RTF格式转化问题
- 利用jpeglib压缩和解压图像为jpg格式
- WPF richtextbox
- 将Rtf转换为Html格式
- RTF转换为HTML格式(java)
- RichTextBox文本保存为WORD格式
- Oracle中查询当前时间并转化为特定格式
- Struts2 action接收json格式字符串并转化为Map
- java获取excel中数据,并转化为JSON格式
- <转>Installing Ubuntu 9.04 on a ThinkPad T60
- JsonUtil JSONObject、JavaBean、Model、list相互转化
- struts2验证器
- [Firefox][扩展系列][02]向Firefox说声HelloWorld
- Sybase(sqlanywhere)比较应用版本大小数据库存储过程
- WPF RichTextBox转化为RTF格式,并进行压缩、解压
- 流浪汉被撞伤 夫妇为流浪汉找到家人并踪助警方逮到肇事车
- linux 下Weblogic 10.3 安装与配置
- 沙漏静止的唯美伤感空间日志推荐:如果,你未娶,我未嫁
- UBUNTU 用户及用户组管理
- [Firefox][扩展系列][01]开发环境准备
- 解决:从byte[]到string再到byte[]导致文件失真
- Objective-C之@property详解
- Pipe实现分析